- 结构体
- 1、结构体的基本概念
- 2、结构体定义和使用
- 3、结构体数组
- 4、结构体指针
- 5、结构体嵌套结构体
- 6、结构体做函数参数
- 7、结构体中const使用场景
- 8、结构体的案例
- 8.1 案例一
- 8.2 案例二
2、结构体定义和使用结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:struct 结构体名 {结构体成员列表};
#includeusing namespace std; #include //使用string字符 //1、创建学生数据类型:学生包括(姓名、年龄、分数) //自定义数据类型,一些类型集合组成的一个类型 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }s3;//顺便创建结构体变量 //2、通过学生类型创建具体学生 int main() { //2.1 struct Student s1; //结构体关键字struct可以省略 struct Student s1; //给s1属性赋值,通过 . 访问结构体变量中的变量 s1.name = "张三"; s1.age = 18; s1.score = 100; cout << s1.name << "---" << s1.age << "---" << s1.score << endl; //2.2 struct Student s1={...}; struct Student s2 = { "李四", 20, 80 }; cout << s2.name << "---" << s2.age << "---" << s2.score << endl; //2.3 在定义结构体时顺便创建结构体变量 s3.name = "王五"; s3.age = 28; s3.score = 10; cout << s3.name << "---" << s3.age << "---" << s3.score << endl; system("pause"); return 0; }
输出:
张三---18---100 李四---20---80 王五---28---103、结构体数组
作用:将自定义的结构体放到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={{}, {}, …{}}
#includeusing namespace std; #include //使用string字符 //结构体数组 //1、定义结构体 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }; //2、通过学生类型创建具体学生 int main() { //2、创建结构体数组 struct Student stuArray[3] { {"zs",18,100}, {"ls", 20, 50 }, {"ww",22,80} }; cout << stuArray << endl; //3、给结构体数组中的元素赋值 stuArray[2].name = "zl"; //4、遍历结构体数组 for (int i = 0; i < 3; i++) { cout << "姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl; } system("pause"); return 0; }
输出:
00EFFC8C 姓名:zs年龄:18分数:100 姓名:ls年龄:20分数:50 姓名:zl年龄:22分数:804、结构体指针
作用:通过指针访问结构体中的成员
利用操作符 -> 可以通过结构体指针访问结构体属性
#includeusing namespace std; #include //使用string字符 //结构体指针 //1、定义结构体 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }; int main() { //1、创建学生结构体变量 struct可以省略 struct Student s = { "李四", 20, 80 }; //2、通过指针指向结构体变量 Student* p = &s; //3、通过指针访问结构体变量中的数据 cout << "p:" << p << endl; //通过结构体指针访问结构体属性,需要使用 -> 符号 cout << "name:" << p->name << "age:" << p->age << "score:" << p->score << endl; system("pause"); return 0; }
输出:
p:00B7F728 name:李四age:20score:805、结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
在结构体中可以定义另一个结构体作为成员,用来解决实际问题。
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#includeusing namespace std; #include //使用string字符 //结构体嵌套结构体 // 学生的结构体 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }; //老师结构体 struct Teacher { //成员列表 //编号 int id; //姓名 string name; //年龄 int age; struct Student stu;//辅导的学生 }; int main() { //创建老师 Teacher t; t.id = 10000; t.name = "ww"; t.age = 30; t.stu.name = "xw"; t.stu.age = 18; t.stu.score = 90; cout << "teacher name:" << t.name << " student name:" << t.stu.name << endl; system("pause"); return 0; }
输出:
teacher name:ww student name:xw6、结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
1、值传递
2、地址传递
#includeusing namespace std; #include //使用string字符 // 学生的结构体 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }; //打印学生信息的函数 //1 值传递 void printStudent1(struct Student s) { s.age = 100; cout << "子函数1中的姓名:" << s.name << " age:" << s.age << " score:" << s.score << endl; } //2 地址传递 void printStudent2(struct Student* s) { s->age = 200; cout << "子函数2中的姓名:" << s->name << " age:" << s->age << " score:" << s->score << endl; } int main() { //结构体做函数参数 //将学生传入到一个参数中,打印学生身上的所有信息 //创建结构体变量 struct Student s1; s1.name = "张三"; s1.age = 18; s1.score = 100; printStudent1(s1); printStudent2(&s1); cout << "main函数:" << s1.age << endl; system("pause"); return 0; }
输出:
子函数1中的姓名:张三 age:100 score:100 子函数2中的姓名:张三 age:200 score:100 main函数:2007、结构体中const使用场景
作用:用const防止误操作
#includeusing namespace std; #include //使用string字符 //const的使用场景 struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int score; }; //将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本 void printStudent1(const Student *s) { //s->150;//加const之后,就不可以修改,防止我们的误操作 cout << "姓名:" << s->name << " age:" << s->age << " score:" << s->score << endl; } int main() { //创建结构体变量 struct Student s1; s1.name = "张三"; s1.age = 18; s1.score = 100; printStudent1(&s1); cout << "main函数的年龄:" << s1.age << endl; system("pause"); return 0; }
输出:
姓名:张三 age:18 score:100 main函数的年龄:188、结构体的案例 8.1 案例一
示意图:
#includeusing namespace std; #include //使用string字符 #include // 学生的结构体 struct Student { //姓名 string sName; //分数 int score; }; // 老师的结构体定义 struct teacher { //姓名 string tName; //学生的数组 struct Student sArray[5]; }; //给老师和学生赋值的函数 void allocateSpace(struct teacher tArray[], int len) { string name_seed = "ABCDE"; //开始给老师赋值 for (int i = 0; i < len;i++) { tArray[i].tName = "teacher_"; tArray[i].tName +=name_seed[i]; //通过循环给每个老师带的学生进行赋值 for (int j = 0; j < 5; j++) { tArray[i].sArray[j].sName = "student_"; tArray[i].sArray[j].sName += name_seed[j]; //int random=rand()%60;//0-59的随机值; int random = rand() % 60 + 40;//40-99的随机值 tArray[i].sArray[j].score = random; } } } //打印所有信息 void printInfo(struct teacher tArray[], int len) { for (int i = 0; i < len; i++) { cout << "teacher name:" << tArray[i].tName << endl; for (int j = 0; j < 5; j++) { cout << "tstudent name:" << tArray[i].sArray[j].sName << " student score:" << tArray[i].sArray[j].score << endl; } } } int main() { //随机数种子 srand((unsigned int)time(NULL)); //1、创建3名老师的数组 struct teacher tArray[3]; //2、通过函数给3名老师的信息赋值,并给老师带的学生赋值 int len = sizeof(tArray) / sizeof(tArray[0]); allocateSpace(tArray, len); //3、打印所有老师和带的学生的信息 printInfo(tArray, len); system("pause"); return 0; }
输出:
teacher name:teacher_A
student name:student_A student score:95
student name:student_B student score:97
student name:student_C student score:87
student name:student_D student score:89
student name:student_E student score:67
teacher name:teacher_B
student name:student_A student score:81
student name:student_B student score:51
student name:student_C student score:53
student name:student_D student score:58
student name:student_E student score:62
teacher name:teacher_C
student name:student_A student score:96
student name:student_B student score:90
student name:student_C student score:71
student name:student_D student score:83
student name:student_E student score:53
8.2 案例二
#includeusing namespace std; #include //使用string字符 //1、设计英雄结构体 //英雄的结构体 struct Hero { //姓名 string name; //年龄 int age; //性别 string sex; }; //冒泡排序 void bubbleSort(struct Hero hArray[], int len) { for (int i = 0; i < len-1; i++) { for (int j = 0; j < len - i-1; j++) { if (hArray[j].age > hArray[j + 1].age) { struct Hero temp = hArray[j]; hArray[j] = hArray[j + 1]; hArray[j + 1] = temp; } } } } //打印输出 void printArray(struct Hero hArray[], int len) { for (int i = 0; i < len; i++) { //疑问:为什么不能这样打印呢:cout< //2、创建数组存放5名英雄 struct Hero hArray[5] { {"刘备", 23, "男"}, {"关羽", 22, "男"}, {"张飞", 21, "男"}, {"赵云", 18, "男"}, {"曹操", 28, "男"}, }; int len = sizeof(hArray) / sizeof(hArray[0]); //3、对数组进行排序,按年龄进行升序 bubbleSort(hArray, len); //4、打印输出 printArray(hArray, len); system("pause"); return 0; }
输出:
name:赵云 age:18 sex:男 name:张飞 age:21 sex:男 name:关羽 age:22 sex:男 name:刘备 age:23 sex:男 name:曹操 age:28 sex:男 请按任意键继续. . .



