C++学习日志 14
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] ={ {},{},...{} }
示例:
#include<iostream>
using namespace std;
#include<string>
struct Student {
string name;
int age;
int score;
};
int main() {
struct Student stuarray[3] = {
{"张三",18,100},
{"李四",19,96},
{"王五",20,97},
};
stuarray[2].name = "赵六";
stuarray[2].age = 30;
stuarray[2].score = 60;
for (int i = 0; i < 3; i++) {
cout << "姓名:" << stuarray[i].name << " 年龄:" << stuarray[i].age << " 分数:" << stuarray[i].score << endl;
}
system("pause");
return 0;
}
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] ={ {},{},...{} }
示例:
#include<iostream>
using namespace std;
#include<string>
struct Student {
string name;
int age;
int score;
};
int main() {
struct Student stuarray[3] = {
{"张三",18,100},
{"李四",19,96},
{"王五",20,97},
};
stuarray[2].name = "赵六";
stuarray[2].age = 30;
stuarray[2].score = 60;
for (int i = 0; i < 3; i++) {
cout << "姓名:" << stuarray[i].name << " 年龄:" << stuarray[i].age << " 分数:" << stuarray[i].score << endl;
}
system("pause");
return 0;
}
全部评论
相关推荐

点赞 评论 收藏
分享
06-23 16:15
西南科技大学 Web前端 点赞 评论 收藏
分享