064.结构体-结构体数组

#include <iostream>
using namespace std;

//1.定义结构体
struct Student
{
    //姓名
    string name;
    //年龄
    int age;
    //分数
    int score;
};
int main()
{
    //2.创建结构体数组
    Student stuarr[3] =
    {
        {"张三",18,100},
        {"李四",28,70},
        {"王五",10,500},
    };

    //3.给结构体数组中元素赋值
    stuarr[2].name = "赵柳";
    stuarr[2].score = 100;
    stuarr[2].age = 50;

    //4.遍历结构体数组
    for (size_t i = 0; i < 3; i++)
    {
        cout << "姓名:" << stuarr[i].name
             << "年龄:" << stuarr[i].age
             << "分数:" << stuarr[i].score << endl;
    }

    system("pause");
    return 0;
}

 

posted @ 2021-09-04 16:32  梦之心  阅读(79)  评论(0编辑  收藏  举报