C++基础之结构体数组

    技术2025-03-03  6

    结构体数组:将自定义的结构体放入到数组中方便维护。

    #include <iostream> using namespace std; #include<string> /* 定义一个结构体 */ struct Student { string name; int age; int score; }; int main() { /* 创建结构体数组并赋值 */ struct Student stuArray[3] = { {"小明",19,60}, {"小红",18,70}, {"小军",20,100} }; /* 修改结构体数组中的元素的值 */ stuArray[2].age = 90; /* 遍历结构体数组 */ int length = sizeof(stuArray) / sizeof(stuArray[0]);//数组长度:3 for (int i = 0; i < length; i++) { cout << stuArray[i].name << stuArray[i].age << stuArray[i].score << endl; } system("pause"); return 0; }
    Processed: 0.008, SQL: 9