#include <iostream>
using namesapce std;
#include <string>
1、定义结构体
struct Student{
String name;
int age;
int score;
};
- 作用:将自定义的结构体放入到数组中方便维护
2、创建结构体数组
struct Student stuArray[3]=
{
{"张三",18,100},
{"李四",20,99},
{"王五",38,66}
};
3、给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
4、访问结构体数组中的值
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
}