在C++中初始化结构数组可以通过以下几种方式:
struct Person {
std::string name;
int age;
};
Person people[3];
people[0] = {"Alice", 25};
people[1] = {"Bob", 30};
people[2] = {"Charlie", 35};
struct Person {
std::string name;
int age;
};
Person people[3] = {
{"Alice", 25},
{"Bob", 30},
{"Charlie", 35}
};
struct Person {
std::string name;
int age;
Person() {} // 默认构造函数
};
Person people[3];
以上是在C++中初始化结构数组的几种常见方式。根据具体的需求和场景,选择适合的方式进行初始化。
领取专属 10元无门槛券
手把手带您无忧上云