简体   繁体   English

在结构中初始化结构向量

[英]Initializing a struct vector in a struct

Reading a structured file of temperature readings Example - From pages 369–370 of Programming: Principles and Practices using C++: 读取温度读数的结构文件示例-从369-370页的编程:使用C ++的原理和实践:

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

That is, a Day has 24 hours, each initialized to 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 }; 

We “waste” day[0] to keep the code simple. 我们“浪费”一天[0]以保持代码简单。

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

Each class is basically a simple vector of “parts,” and Month and Year have an identifying member month and year, respectively. 每个类基本上都是“部分”的简单向量,“月”和“年”分别具有标识成员“月”和“年”。

Shouldn't brackets be used instead of curly braces, if the goal is to create a vector of type day/month that has 32/12 members? 如果目标是创建具有32/12成员的day / month类型的向量,则不应该使用方括号代替大括号吗?

The curly braces lets the compiler know that you want to initialize using an initializer list. 花括号使编译器知道您要使用初始化列表进行初始化。

Brackets are used to access vector elements. 括号用于访问矢量元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM