在结构中初始化结构向量

时间:2018-01-29 00:08:54

标签: c++ vector struct

读取温度读数的结构化文件示例 - 来自编程:使用C ++的原理和实践的第369-370页:

struct Day {
vector<double> hour {vector<double>(24,not_a_reading)};
};
     

也就是说,一天有24小时,每个都初始化为not_a_reading。

struct Month { // a month of temperature readings
int month {not_a_month}; // [0:11] January is 0
vector<Day> day {32}; // [1:31] one vector of readings per day
};
     

我们“浪费”了一天[0]以保持代码简单。

struct Year { // a year of temperature readings, organized by month
int year; // positive == A.D.
vector<Month> month {12}; // [0:11] January is 0
};
     

每个类基本上都是“部分”和月份和年份的简单向量   分别有一个月份和年份的识别成员。

如果目标是创建具有32/12成员的日/月类型的向量,则不应使用括号而不是花括号吗?

1 个答案:

答案 0 :(得分:0)

大括号让编译器知道您要使用初始化列表进行初始化。

括号用于访问矢量元素。

相关问题