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;
}
全部评论

相关推荐

06-20 19:40
中原工学院 Java
网络存储:十几天不会让你拉人办卡就结束了吧?
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务