语法:struct 结构体名 数组名[元素个数] = { {}, {}, ..., {} }
作用:将自定义的结构体放入到数组中方便维护
1.定义结构体
struct Student
{
string name;
int age;
int score;
};
2.创建结构体数组
int main()
{
//创建结构体数组和赋值
struct Student stuArray[3] =
{
{"张三", 18, 100},
{"李四", 20, 99},
{"王五", 30, 80},
};
}
3.给结构体数组中的元素另一种赋值方式
stuArray[2].name = "赵四";
stuArray[2].age = 60;
stuArray[2].score = 70;
4.遍历结构体
for (int i = 0; i < 3; i++)
{
cout << "姓名" << stuArray[i].name
<< "年龄" << stuArray[i].age
<< "分数" << stuArray[i].score << endl
}