作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名 [元素个数]={{},{},{}....};
示例:
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student
{
string name;
int age;
int height;
};
int main(){
//2、创建结构体数组
//3、给结构体数组元素赋值
struct Student StuArray[3]=
{
{"张三",17,180},
{"李四",20,178},
{"王五",34,178}
};
//3.1修改结构体数组
StuArray[2].name="赵六";
StuArray[2].age=24;
//4、遍历结构体数组
for(int i=0;i<3;i++){
cout<<"姓名:"<<StuArray[i].name<<" 年龄:"<<StuArray[i].age<<" 身高:"<<StuArray[i].height<<endl;
}
system("pause");
return 0;
}
结果运行:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6YlnJmra-1615465181428)(C:\Users\徐老帅哥\AppData\Roaming\Typora\typora-user-images\image-20210311201839758.png)]