文章目录
- 7.4 结构体指针
- 7.5 结构体嵌套结构体
- 7.6 结构体做函数参数
- 7.7 结构体中const使用场景
- 7.8 结构体案例
- 案例1
- 谢谢你的点赞,评论和转发 ( ^ o ^)/~。
7.4 结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符 - >可以通过结构体指针访问结构体属性
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <iostream> #include<string> using namespace std; struct Person { //结构体成员 //姓名 string name; //身高 float heigth; //体重 int weigth; }; int main52() { //1.创建个人结构体变量 struct Person me = { "智多星",180,75 }; //int* p = &me;错误 //2.通过指针指向结构体变量 struct Person* p = &me; //将me变量的地址赋予指针p //3.通过指针访问结构体变量中的数据 cout << " name : " << p->name << " Heigth :" << p->heigth << " weigth : " << p->weigth << endl; system("pause"); return 0; }
注意:通过结构体指针访问结构体中的属性/成员,需要利用" - >"
7.5 结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
**例如 **:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include<iostream> #include<string> using namespace std; //定义学生的结构体 struct student { //学生结构体成员 string name; int age; int sore; }; //定义老师的结构体之前要先定义老师的结构体才能引用学生结构体 struct teacher { //成员列表 int id; //职工编号 string name; //教师姓名 int age; //教师年龄 struct student s1; //定义学生结构体变量 }; int main() { struct teacher t1; t1.id = 00324; t1.name = " 陈辉"; t1.age = 45; t1.s1.name = "廖克志"; t1.s1.age = 21; t1.s1.sore = 100; cout << "教师的名字:" << t1.name << "教师的年龄:" << t1.age << "教师编号:"<< t1.id << " 学生名字:" << t1.s1.name << " 学生年龄:" << t1.s1.age << " 学生分数:" << t1.s1.sore << endl; system("pause"); return 0; }
7.6 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
- 值传递
- 地址传递
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include<iostream> using namespace std; #include<string> //1.创建学生数据类型 学生包括(姓名,年龄,分数) struct Student { //成员列表 //姓名 string name; //年龄 int age; //分数 int sore; }; //方法1:值传递 void printStudent1(struct Student s) { s.age = 38; cout << "在值传递函数中 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.sore << endl; } //方法2:地址传递 void printStudent2(struct Student *p) { p->age = 38; cout << "在地址传递函数中 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->sore << endl; } int main() { //2.通过学生类型创建具体学生 //方式1:2.1 struct Student s1 struct Student s1; s1.age = 18; s1.name = "张三"; s1.sore = 98; //printStudent1(s1); printStudent2(&s1); cout << "在main函数中 姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.sore << endl; system("pause"); return 0; }
总结:如果不想修改主函数中的数据,用值传递,反之用地址传递。
7.7 结构体中const使用场景
作用:用const来防止误操作
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include<iostream> #include<string> using namespace std; //定义学生结构体 struct Student { //结构体成员 string name; //姓名 int age; //年龄 int sore; //分数 }; void printStudent(const Student* p) { //p->age = 90; 加入xonst之后,一旦有修改的操作就会报错,可以防止我们的误操作 cout << " 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->sore << endl; } //将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本 int main() { //创建结构体变量 struct Student s = { "张三",30,88 }; //通过函数打印结构体变量信息 printStudent(&s); cout << " main函数中姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.sore << endl; system("pause"); return 0; }
7.8 结构体案例
案例1
学校正在做毕设项目,每位老师带领5名学生,总共有3名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值最终打印出老师数据以及老师所带的学生数据。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#include<iostream> #include<string> using namespace std; #include<ctime> //1.创建老师结构体 struct student { //学生属性 string name; int age = 0; int sore = 0; }; //1.创建学生结构体 struct teacher { string tname; struct student ArryStudent[5]; }; //2.赋值函数 void FuZhi(struct teacher tArry[], int len) { string nameSeed = "ABCDE"; for (int i = 0; i < len; i++) { tArry[i].tname = "Teacher_"; tArry[i].tname += nameSeed[i]; for (int j = 0; j < 5; j++) { tArry[i].ArryStudent[j].name = "Student_"; tArry[i].ArryStudent[j].name += nameSeed[i]; int random = rand() % 61 + 40; //40~100 tArry[i].ArryStudent[j].sore = random; tArry[i].ArryStudent[j].age = 21; } } } void PrintInfo(struct teacher tArry[], int len) { for (int i = 0; i < len; i++) { cout << " 老师姓名:" << tArry[i].tname << endl; for (int j = 0; j < 5; j++) { cout << " t 学生姓名:" << tArry[i].ArryStudent[j].name << " 考试分数:" << tArry[i].ArryStudent[j].sore << " 学生年龄:" << tArry[i].ArryStudent[j].age << endl; } } } int main() { //随机数种子 srand((unsigned int)time(NULL)); //1、创建老师和学生结构体 struct teacher ArryTeacher[3]; //2. 给结构体赋值 int len = sizeof(ArryTeacher) / sizeof(ArryTeacher[0]); FuZhi(ArryTeacher, len); //3.打印输出结构体信息 PrintInfo(ArryTeacher, len); system("pause"); return 0; }
案例2
设计一个英雄的结构体,包括成员姓名,年龄,性别,创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄安装年龄进行升序排序,最终打印排序后的结果。
复制代码
1
2
3
4
5
6{"刘备",23,"男"} {"关羽",24,"男"} {"张飞",22,"男"} {"貂蝉",19,"女"} {"赵云",20,"男"}
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#include<iostream> #include<string> using namespace std; //1 定义英雄结构体 struct Hero { string name; int age; string sex; }; //3.排序函数 void SortHero(struct Hero Arr[], int len) { for (int i = 0; i < len -1; i++) { for (int j = 0; j < len - i - 1; j++) { if (Arr[j].age > Arr[j+1].age ) { struct Hero temp; temp = Arr[j]; Arr[j] = Arr[j + 1]; Arr[j + 1] = temp; } } } } //打印函数 void printHero(struct Hero Arr[], int len) { for (int i = 0; i < 5; i++) { cout << " 姓名:" << Arr[i].name << " 年龄:" << Arr[i].age << " 性别:" << Arr[i].sex << endl; } } int main() { //1.定义英雄结构体 //2.创建英雄数组 struct Hero ArryHero[5] = { {"刘备",23,"男"}, {"关羽",24,"男"}, {"张飞",22,"男"}, {"貂蝉",19,"女"}, {"赵云",20,"男"}, }; //3.按照英雄年龄进行排序 cout << "排序前结果:" << endl; for (int i = 0; i < 5; i++) { cout << " 姓名:" << ArryHero[i].name << " 年龄:" << ArryHero[i].age << " 性别:" << ArryHero[i].sex << endl; } //4.打印输出排序后结果 cout << "排序后结果:" << endl; printHero(ArryHero, 5); system("pause"); return 0; }
谢谢你的点赞,评论和转发 ( ^ o ^)/~。
最后
以上就是呆萌枫叶最近收集整理的关于15C++结构体基础知识点(二)的全部内容,更多相关15C++结构体基础知识点(二)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复