结构体的定义和使用

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>
using namespace std;
#include<string>
//语法struct 类型名称{成员列表}
struct Student
{
//成员列表
string name;
//姓名
int age;
//分数
int score;

};
//2、通过学生类型创建具体学生

//2.1 struct Student s1
//2.2 struct Student s2={....}
//2.3 在定义结构体时顺便创建结构体变量
int main()
{
//2.1 struct Student s1
struct Student s1;
//给s1属性赋值,通过.访问结构体变量中的属性
s1.name="张三";
s1.age=18;
s1.score=100;
cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<"分数"<<s1.score<<endl;/*输出字符串的时候要包含头文件#include<string>*/
//2.2 struct Student s2={...}
struct Student s2={"李四",19,80};
cout<<"姓名"<<s2.name<<"年龄"<<s2.age<<"分数"<<s2.score<<endl;
}
//2.3顺便创建结构体变量

结构体数组

image-20220123213915794

image-20220123214445687


结构体指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
int main()
{
struct Student s={"张三",18,100};//struct可省略
//通过指针指向结构体变量
struct student *p=&s;//struct可省略
//通过指针访问结构体变量中的数据
cout<<"姓名"<<p->name<<"年龄"<<p->age<<"分数"<<p->score;
system("pause");
return 0;
}

结构体嵌套结构体

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
#include<iostream>
using 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=10086;
t.name="老王";
t.age=50;
t.stu.name="小王";
t.stu.age=18;
t.stu.score=88;
cout<<"老师的姓名 "<<t.name<<"老师的编号 "<<t.id<<"老师的年龄 "<<t.age<<endl<<"老师辅导的学生姓名 "<<t.stu.name<<"学生年龄 "<<t.stu.age<<"学生的成绩 "<<t.stu.score;
return 0;
}

结构体做函数参数

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
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
void printstudent1(student s)
{
cout << "子函数1: " << " 姓名 " << s.name << " 年龄 " << s.age << " 分数 " << s.score << endl;
};
void printstudent2(struct student * p)
{
cout << "子函数2: " << " 姓名 " << p->name << " 年龄 " << p->age << " 分数 " << p->score << endl;
}
int main()
{
student s;
s.name = "张三";
s.age = 18;
s.score = 95;
printstudent1(s);
printstudent2(&s);
return 0;
}

结构体中const使用场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
#include<string>
//const的使用场景
struct student
{
string name;
int age;
int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printstudent(const student *s)
{
//加入const之后,一旦有修改的操作就会报错,可以防止我们误操作
cout<<" 姓名 "<<s->name<<" 年龄 "<<s->age<<" 分数 "<<s->score<<endl;
};
int main()
{
//创建结构体变量
struct student s={"张三",18,75};
printstudent (&s);
return 0;
}

结构体案例1

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
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
//const的使用场景
struct Student
{
string sName;
int age;
int score;
};
struct Teacher
{
string tName;
//学生数组
struct Student sArray[5];
};
//给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[], int len)
{
string nameSeed = "ABCDE";
//给老师开始赋值
for (int i = 0; i < len; i++)
{
tArray[i].tName = "Teacher_";
tArray[i].tName += nameSeed[i];
//通过循环给每名老师所带的学生赋值
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].sName = "Student_";
tArray[i].sArray[j].sName += nameSeed[j];

int random = rand() % 61 + 40; // 40~100,如果后面是100,就是0~99
tArray[i].sArray[j].score = random;
}
}
}
//3、打印所有信息
void printInfo(struct Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师姓名 " << tArray[i].tName << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生姓名 " << tArray[i].sArray[j].sName << " 考试分数 " << tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//随机数种子
//1、创建3名老师的数组
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
printInfo(tArray,len);
//2、通过函数给3名老师的信息赋值
return 0;
}

结构体案例2

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
#include<iostream>
#include<string>
using namespace std;
struct Hero
{
//1、设计英雄结构体
//姓名
string name;
//年龄
int age;
//性别
string sex;
};
//冒泡排序 实现年龄升序排列
void bubbleSort(struct Hero heroArray[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
//如果j下标的元素年龄大于j+1下标元素的年龄,交换两个元素
if (heroArray[j].age > heroArray[j + 1].age)
{
struct Hero temp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = temp;
}
}
}
}
//打印排序后数组中的信息
void printHero(struct Hero heroArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名: " << heroArray[i].name << "年龄:" << heroArray[i].age << "性别: " << heroArray[i].sex << endl;
}

}
int main()
{
//2、创建数组存放5名英雄
struct Hero heroArray[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(heroArray) / sizeof(heroArray[0]);
/*for (int i = 0; i < len; i++)
{
cout << "姓名: " << heroArray[i].name << "年龄:" << heroArray[i].age << "性别: " << heroArray[i].age << endl;
}*/
//3、对数组进行排序
bubbleSort(heroArray, len);
//4、将结果打印输出
printHero(heroArray, len);
return 0;
}