- 一、结构体
- (一)结构体的基本概念
- (二)结构体的定义和使用
- (三)结构体数组
- (四)结构体指针
- (五)结构体嵌套结构体
- (六)结构体做函数参数
- (七)结构体中const使用场景
- 二、结构体案例
- (一)案例一
- (二)案例二
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
(二)结构体的定义和使用练习代码如下:
#include#include using namespace std; //创建学生数据类型 //自定义数据类型:一些内置类型集合组成的一个类型 struct Student { string name;//姓名 int age;//年龄 int score;//分数 }; int main() { //通过学生类型创建出具体学生 //第一种方式: struct Student s1; s1.name = "zhangsan"; s1.age = 18; s1.score = 100; cout << "姓名: " << s1.name << endl; cout << "年龄: " << s1.age << endl; cout << "分数: " << s1.score << endl; return 0; }
#include#include using namespace std; //创建学生数据类型 //自定义数据类型:一些内置类型集合组成的一个类型 struct Student//struct关键字不可以省略 { string name;//姓名 int age;//年龄 int score;//分数 }; int main() { //第二种定义方式: struct Student s2 = { "lisi",20,98 };//struct关键字可以省略 cout << "姓名: " << s2.name << endl; cout << "年龄: " << s2.age << endl; cout << "分数: " << s2.score << endl; return 0; }
#include#include using namespace std; //创建学生数据类型 //自定义数据类型:一些内置类型集合组成的一个类型 struct Student { string name;//姓名 int age;//年龄 int score;//分数 }s3; int main() { //第三种定义方式 s3.name = "wangwu"; s3.age = 21; s3.score = 67; cout << "姓名: " << s3.name << endl; cout << "年龄: " << s3.age << endl; cout << "分数: " << s3.score << endl; return 0; }
注意:
- 定义结构体时struct关键字不可以省略
- 创建结构体变量时struct关键字可以省略
- 通过.的方式访问结构体的成员变量
作用:将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[元素个数] =
{{},{},...,{}}
练习代码如下:
#include(四)结构体指针#include using namespace std; //创建学生数据类型 //自定义数据类型:一些内置类型集合组成的一个类型 struct Student { string name;//姓名 int age;//年龄 int score;//分数 }; int main() { //创建结构体数组,并对数组元素赋值 struct Student stuarr[3] = { {"zhangsan",18,100}, {"lisi",19,98}, {"wangwu",20,87} }; for (int i = 0; i < 3; i++) { cout << "姓名: " << stuarr[i].name << " " << "年龄: " << stuarr[i].age << " " << "分数: " << stuarr[i].score << endl; } stuarr[0].age = 99; stuarr[0].score = 99; cout << "********************************" << endl; for (int i = 0; i < 3; i++) { cout << "姓名: " << stuarr[i].name << " " << "年龄: " << stuarr[i].age << " " << "分数: " << stuarr[i].score << endl; } return 0; }
作用:通过指针访问结构体中的成员
- 利用操作符->可以通过结构体指针访问结构体属性
练习代码如下:
#include(五)结构体嵌套结构体#include using namespace std; //创建学生数据类型 //自定义数据类型:一些内置类型集合组成的一个类型 struct Student { string name;//姓名 int age;//年龄 int score;//分数 }; int main() { //创建学生结构体变量 struct Student s = { "zhangsna",18,100 }; //通过指针指向结构体变量 struct Student* p = &s; //通过指针访问结构体变量中的数据 cout << "姓名:" << p->name << " " << "年龄:" << p->age << " " << "分数:" << p->score << endl; return 0; }
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
练习代码如下:
#include(六)结构体做函数参数#include using namespace std; 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 = "laowang"; t.age = 50; t.stu.age = 18; t.stu.name = "xiaowang"; t.stu.score = 100; cout << "老师的姓名:" << t.name << endl; cout << "老师的编号:" << t.id << endl; cout << "老师的年龄:" << t.age << endl; cout << "学生姓名:" << t.stu.name << endl; cout << "学生年龄:" << t.stu.age << endl; cout << "学生分数:" << t.stu.score << endl; return 0; }
作用:将结构体作为参数向函数中传递
传递方式有两种:
- 值传递
- 地址传递
练习代码如下:
#include#include using namespace std; //定义学生结构体 struct student { //成员列表 string name;//学生姓名 int age;//学生年龄 int score;//学生分数 }; //打印学生信息函数 void PrintStudent(struct student s)//值传递 { s.age = 100; cout << "子函数中的信息为:" << s.name << " " << s.age << " " << s.score << endl; } void PrintStudent2(struct student* s) { s->age = 100; cout << "子函数2中打印的信息" << s->name << " " << s->age << " " << s->score << endl; } int main() { //结构体做函数参数 //将学生传入到一个参数中,打印学生身上的所有信息 struct student s; s.name = "zhangsan"; s.age = 20; s.score = 100; cout << "main函数中打印的结果:" << s.name << " " << s.age << " " << s.score << endl; PrintStudent(s); cout << "main函数中打印的结果:" << s.name << " " << s.age << " " << s.score << endl; PrintStudent2(&s); cout << "main函数中打印的结果:" << s.name << " " << s.age << " " << s.score << endl; return 0; }
注意:如果不想修改主函数中的数据就用值传递,反之就用地址传递
作用:用const来防止误操作
练习代码如下:
#include二、结构体案例 (一)案例一#include using namespace std; struct student { string name;//姓名 int age;//年龄 int score;//分数 }; //将函数中的形参改为指针,可以减少内存空间开销,而且不会赋值新的副本出来 //给形参加入const之后,一旦有修改的操作就会报错,可以防止我们误操作 void PrintStudents(const student *s) { //有const修饰,可读不可写 //s->age = 190;//error cout << "姓名:" << s->name << " " << "年龄:" << s->age << " " << "得分:" << s->score << endl; } int main() { struct student s = { "zhangsan",18,99 }; //通过函数打印结构体变量的信息 PrintStudents(&s); return 0; }
案例描述:
- 学校正在做毕设项目,每名老师带领五个学生,总共有三名老师,需求如下:
- 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
- 学生的成员有姓名、考试分数、创建数组存放三名老师,通过函数给每个老师及所带学生赋值
- 最终打印出老师数据以及老师所带学生数据
案例代码如下:
#include(二)案例二#include #include using namespace std; //学生的结构体 struct Student { string sname;//学生姓名 int score;//考试分数 }; //老师的结构体 struct Teacher { string tname;//老师姓名 Student arr[5];//老师所带的5名学生 }; //给老师和学生赋值的函数 void allocate(Teacher tarr[], int len) { string nameseed = "abcde"; for (int i = 0; i < len; i++) { tarr[i].tname = "teacher_"; tarr[i].tname += nameseed[i]; //给每名老师所带的学生赋值 for (int j = 0; j < 5; j++) { tarr[i].arr[j].sname = "student_"; tarr[i].arr[j].sname += nameseed[j]; int random = rand() % 61 + 40;//40~100 tarr[i].arr[j].score = random; } } } void Print(Teacher tarr[], int len) { for (int i = 0; i < len; i++) { cout << "老师姓名:" << tarr[i].tname << endl; for (int j = 0; j < 5; j++) { cout << "t学生姓名:" << tarr[i].arr[j].sname <<" " "考试分数:" << tarr[i].arr[j].score << endl; } cout << "**********************************" << endl; } } int main() { //随机数种子 srand((unsigned int)time(NULL)); //创建3名老师的数组 Teacher tarr[3]; //通过函数给3名老师的信息赋值,并且给老师所带学生信息赋值 int len = sizeof(tarr) / sizeof(tarr[0]); allocate(tarr, len); //打印所有老师及所带学生信息 Print(tarr, len); return 0; }
案例描述:
- 设计一个英雄的结构体,包括成员姓名、年龄、性别,创建结构体数组,数组中存放5名英雄
- 通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果
案例代码如下:
#include#include using namespace std; //设计英雄结构体 struct Hero { string name;//姓名 int age;//年龄 string sex;//性别 }; //冒泡排序 void BubbleSort(Hero arr[], int len) { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j].age > arr[j + 1].age) { Hero tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } //打印输出函数 void Print(Hero arr[], int len) { cout << "输出排序后的结果:" << endl; for (int i = 0; i < len; i++) { cout << "姓名:" << arr[i].name <<" "; cout << "年龄:" << arr[i].age << " "; cout << "性别:" << arr[i].sex << endl; } } int main() { //创建数组存放5名英雄 struct Hero arr[5] = { {"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"} }; int len = sizeof(arr) / sizeof(arr[0]); cout << "输出排序前的结果:" << endl; for (int i = 0; i < len; i++) { cout << "姓名:" << arr[i].name << " "; cout << "年龄:" << arr[i].age << " "; cout << "性别:" << arr[i].sex << endl; } //对数组进行排序,按照年龄进行升序排序 BubbleSort(arr, len); //将排序后结果打印输出 Print(arr, len); return 0; }
✨✨✨
加油!


![【C++】学习笔记[五] 【C++】学习笔记[五]](http://www.mshxw.com/aiimages/31/302880.png)
