1 結構體基本概念
結構體屬於用戶==自定義的數據類型==,允許用戶存儲不同的數據類型,不同於內置的類似於int,string這些數據類型。
2 結構體定義和使用
語法:struct 結構體名 { 結構體成員列表 };
通過結構體創建變量的方式有三種:
- struct 結構體名 變量名
- struct 結構體名 變量名 = { 成員1值 , 成員2值...}
- 定義結構體時順便創建變量
示例:
//結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }stu3; //結構體變量創建方式3 int main() { //結構體變量創建方式1 struct student stu1; //struct 關鍵字可以省略 stu1.name = "張三"; stu1.age = 18; stu1.score = 100; cout << "姓名:" << stu1.name << " 年齡:" << stu1.age << " 分數:" << stu1.score << endl; //結構體變量創建方式2 struct student stu2 = { "李四",19,60 }; cout << "姓名:" << stu2.name << " 年齡:" << stu2.age << " 分數:" << stu2.score << endl; stu3.name = "王五"; stu3.age = 18; stu3.score = 80; cout << "姓名:" << stu3.name << " 年齡:" << stu3.age << " 分數:" << stu3.score << endl; system("pause"); return 0; }
總結1:定義結構體時的關鍵字是struct,不可省略
總結2:創建結構體變量時,關鍵字struct可以省略
總結3:結構體變量利用操作符 ''.'' 訪問成員
3 結構體數組
**作用:**將自定義的結構體放入到數組中方便維護
語法: struct 結構體名 數組名[元素個數] = { {} , {} , ... {} }
示例:
//結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 } int main() { //結構體數組 struct student arr[3]= { {"張三",18,80 }, {"李四",19,60 }, {"王五",20,70 } }; for (int i = 0; i < 3; i++) { cout << "姓名:" << arr[i].name << " 年齡:" << arr[i].age << " 分數:" << arr[i].score << endl; } system("pause"); return 0; }
4 結構體指針
**作用:**通過指針訪問結構體中的成員
- 利用操作符
->
可以通過結構體指針訪問結構體屬性
//結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }; int main() { struct student stu = { "張三",18,100, }; struct student * p = &stu; p->score = 80; //指針通過 -> 操作符可以訪問成員 cout << "姓名:" << p->name << " 年齡:" << p->age << " 分數:" << p->score << endl; system("pause"); return 0; }
總結:結構體指針可以通過 -> 操作符 來訪問結構體中的成員
C++結構體與類的區別
5 結構體嵌套結構體
作用: 結構體中的成員可以是另一個結構體
**例如:**每個老師輔導一個學員,一個老師的結構體中,記錄一個學生的結構體
//學生結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }; //教師結構體定義 struct teacher { //成員列表 int id; //職工編號 string name; //教師姓名 int age; //教師年齡 struct student stu; //子結構體 學生 }; int main() { struct teacher t1; t1.id = 10000; t1.name = "老王"; t1.age = 40; t1.stu.name = "張三"; t1.stu.age = 18; t1.stu.score = 100; cout << "教師 職工編號: " << t1.id << " 姓名: " << t1.name << " 年齡: " << t1.age << endl; cout << "輔導學員 姓名: " << t1.stu.name << " 年齡:" << t1.stu.age << " 考試分數: " << t1.stu.score << endl; system("pause"); return 0; }
6 結構體做函數參數
**作用:**將結構體作為參數向函數中傳遞
傳遞方式有兩種:
- 值傳遞
- 地址傳遞
//學生結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }; //值傳遞 void printStudent(student stu ) { stu.age = 28; cout << "子函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl; } //地址傳遞 void printStudent2(student *stu) { stu->age = 28; cout << "子函數中 姓名:" << stu->name << " 年齡: " << stu->age << " 分數:" << stu->score << endl; } int main() { student stu = { "張三",18,100}; //值傳遞 printStudent(stu); cout << "主函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl; cout << endl; //地址傳遞 printStudent2(&stu); cout << "主函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl; system("pause"); return 0; }
總結:如果不想修改主函數中的數據,用值傳遞,反之用地址傳遞
C++函數調用之——值傳遞、指針傳遞、引用傳遞
內置類型和結構體類型作為函數參數傳遞時的共同點
7 結構體中 const使用場景
**作用:**用const來防止誤操作
示例:
//學生結構體定義 struct student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }; //const使用場景 void printStudent(const student *stu) //加const防止函數體中的誤操作 { //stu->age = 100; //操作失敗,因為加了const修飾 cout << "姓名:" << stu->name << " 年齡:" << stu->age << " 分數:" << stu->score << endl; } int main() { student stu = { "張三",18,100 }; printStudent(&stu); system("pause"); return 0; }
使用指針傳遞參數有一個很大的原因是提高效率,因為不管信息多大,通過指針傳遞,在32位下都只會傳遞4個字節,比單純將整個結構體傳入性能要高。