原創 閆小林 C語言入門到精通 2020-12-30ios
收錄於話題數組
#小林C++代碼基礎ide
95個函數
點擊上方「C語言入門到精通」,選擇置頂spa
第一時間關注程序猿身邊的故事3d
做者blog
閆小林圖片
白天搬磚,晚上作夢。我有故事,你有酒麼?ci
C++結構體數組
it
C++結構體數組與之前介紹過的數值型數組的不一樣之處在於:每一個數組元素都是一個結構體類 型的數據,它們都分別包括各個成員項。
C++結構體數組定義
C++結構體數組的定義和定義結構體變量的方法相仿,只需聲明其爲數組便可
struct Student{ //自定義結構體變量
int num;//學號
char sex;//性別
int age;//年齡
};
Student stu[5];//定義Student類型的結構體數組
struct Student{ //自定義結構體變量
int num;//學號
char sex;//性別
int age;//年齡
}stu[5];//定義Student類型的結構體數組
struct { //自定義結構體變量
int num;//學號
char sex;//性別
int age;//年齡
}stu[5];//定義Student類型的結構體數組
struct Student{ //自定義結構體變量
int num;//學號
char sex;//性別
int age;//年齡
}stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
#include<iostream>//預處理
using namespace std;//命名空間
int main()//主函數
{
struct Student{ //自定義結構體變量
int num;//學號
char sex;//性別
int age;//年齡
}stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
for(int i=0;i<3;i++)//循環輸出結構體數組信息
{
cout<<stu[i].num<<endl;//輸出學號
cout<<stu[i].sex<<endl;//輸出性別
cout<<stu[i].age<<endl;//輸出年齡
cout<<"---------"<<endl;//隔開
}
return 0; //函數返回值爲0;
}
1001
M
21
---------
1002
F
18
---------
1003
M
19
---------
--------------------------------Process exited after 0.08727 seconds with return value 0請按任意鍵繼續. . .