1880 字
9 分钟
1.8结构体
8 结构体
8.1 结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
8.2 结构体定义和使用
语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:
- struct 结构体名 变量名
- struct 结构体名 变量名 = { 成员1值 , 成员2值…}
- 定义结构体时顺便创建变量
示例:
#include<iostream>#include<string>
//结构体定义struct student{ //成员列表 std::string name; //姓名 int age; //年龄 int score; //分数}stu3; //结构体变量创建方式3
int main() {
//结构体变量创建方式1 struct student stu1; //struct 关键字可以省略
stu1.name = "张三"; stu1.age = 18; stu1.score = 100;
std::cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << std::endl;
//结构体变量创建方式2 struct student stu2 = { "李四",19,60 };
std::cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << std::endl;
stu3.name = "王五"; stu3.age = 18; stu3.score = 80;
std::cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << std::endl;
return 0;}总结1:定义结构体时的关键字是struct,不可省略
总结2:创建结构体变量时,关键字struct可以省略
总结3:结构体变量利用操作符 ”.” 访问成员
8.3 结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法: struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }
示例:
#include<iostream>#include<string>
//结构体定义struct student{ //成员列表 std::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++) { std::cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << std::endl; }
return 0;}8.4 结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符
->可以通过结构体指针访问结构体属性
示例:
#include<iostream>#include<string>
//结构体定义struct student{ //成员列表 std::string name; //姓名 int age; //年龄 int score; //分数};
int main() {
struct student stu = { "张三",18,100, };
struct student* p = &stu;
p->score = 80; //指针通过 -> 操作符可以访问成员
std::cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << std::endl;
return 0;}总结:结构体指针可以通过 -> 操作符 来访问结构体中的成员
8.5 结构体嵌套结构体
作用: 结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
示例:
#include<iostream>#include<string>
//学生结构体定义struct student{ //成员列表 std::string name; //姓名 int age; //年龄 int score; //分数};
//教师结构体定义struct teacher{ //成员列表 int id; //职工编号 std::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;
std::cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << std::endl;
std::cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << std::endl;
return 0;}总结:在结构体中可以定义另一个结构体作为成员,用来解决实际问题
8.6 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
- 值传递
- 地址传递
示例:
#include<iostream>#include<string>
//学生结构体定义struct student{ //成员列表 std::string name; //姓名 int age; //年龄 int score; //分数};
//值传递void printStudent(student stu){ stu.age = 28; std::cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << std::endl;}
//地址传递void printStudent2(student* stu){ stu->age = 28; std::cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << std::endl;}
int main() {
student stu = { "张三",18,100 }; //值传递 printStudent(stu); std::cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << std::endl;
std::cout << std::endl;
//地址传递 printStudent2(&stu); std::cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << std::endl;
return 0;}总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
8.7 结构体中 const使用场景
作用:用const来防止误操作
示例:
#include<iostream>#include<string>
//学生结构体定义struct student{ //成员列表 std::string name; //姓名 int age; //年龄 int score; //分数};
//const使用场景 减少数据的复制开销,改为地址传递节省内存void printStudent(const student* stu) //加const防止函数体中的误操作{ //stu->age = 100; //操作失败,因为加了const修饰 std::cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << std::endl;
}
int main() {
student stu = { "张三",18,100 };
printStudent(&stu);
return 0;}8.8 结构体案例
8.8.1 案例1
案例描述:
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
最终打印出老师数据以及老师所带的学生数据。
示例:
#include<iostream>#include<string>#include <ctime>
struct Student{ std::string name; int score;};
struct Teacher{ std::string name; Student sArray[5];};
// 传入数组等同于指针void allocateSpace(Teacher tArray[], int len){ std::string tName = "教师"; std::string sName = "学生"; std::string nameSeed = "ABCDE"; for (int i = 0; i < len; i++) { tArray[i].name = tName + nameSeed[i];
for (int j = 0; j < 5; j++) { tArray[i].sArray[j].name = sName + nameSeed[j]; tArray[i].sArray[j].score = rand() % 61 + 40; } }}
void printTeachers(Teacher tArray[], int len){ for (int i = 0; i < len; i++) { std::cout << tArray[i].name << std::endl; for (int j = 0; j < 5; j++) { std::cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << std::endl; } }}
int main() {
srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
Teacher tArray[3]; //老师数组
int len = sizeof(tArray) / sizeof(Teacher);
allocateSpace(tArray, len); //创建数据
printTeachers(tArray, len); //打印数据
return 0;}8.8.2 案例2
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
五名英雄信息如下:
{"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"},示例:
#include<iostream>#include<string>
//英雄结构体struct hero{ std::string name; int age; std::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 temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }}
//打印数组void printHeros(hero arr[], int len){ for (int i = 0; i < len; i++) { std::cout << "姓名: " << arr[i].name << " 性别: " << arr[i].sex << " 年龄: " << arr[i].age << std::endl; }}
int main() {
struct hero arr[5] = { {"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"}, };
int len = sizeof(arr) / sizeof(hero); //获取数组元素个数
bubbleSort(arr, len); //排序
printHeros(arr, len); //打印
return 0;}