8 结构体
8.1 结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
8.2 结构体定义和使用
语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式:
(1)struct 结构体名 变量名
(2)struct 结构体名 变量名={成员1值,成员2值。。。}
(3)定义结构体时顺便创建变量
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:创建学生数据类型:(自定义数据类型,一些基本数据类型集合组成的一个类型:struct 类型名称 {成员列表})
struct Student
{
//成员列表
string name;
int age;
int score;
};
struct SS
{
//成员列表
string name;
int age;
int score;
}s3;
//2.使用结构体:通过学生类型创建学生
int main()
{
//2.1 struct Student s1
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
//2.2 struct Student s2={...}
struct Student s2 = { "李四",18,95 };
cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
//2.3 在定义结构体时顺便创建结构体变量
//之前定义了SS结构体时顺便定义了结构体变量
s3.name = "王五";
s3.age = 19;
s3.score = 99;
cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
}
8.3 结构体数组
作用:将自定义的结构体放入数组中方便维护
语法:struct 结构体名 数组名{元素个数}={{},{},…{}}
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:
struct Student
{
//成员列表
string name;
int age;
int score;
};
int main()
{
//2.创建结构体数组
struct Student stuArray[3] =
{
{"张三",17,86},
{"李四",15,87},
{"王五",20,69}
};
//3.结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 30;
stuArray[2].score = 88;
//3.结构体数组遍历
for (int i = 0; i < sizeof(stuArray) / sizeof(stuArray[0]); i++)
{
cout << "姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
}
}
8.4 结构体指针
作用:通过指针访问结构体成员
利用操作符->可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:
struct Student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
int main()
{
//2.创建结构体变量
struct Student s = { "张三",17,86 };
//3. 通过指针指向结构体变量
struct Student* p = &s;
//4.通过指针访问结构体变量中的数据
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
}
8.5 结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:
//创建学生结构体
struct student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
//创建老师结构体
struct teacher
{
int id;
string name;
int age;
struct student stu;//辅导的学生:类型为结构体
};
int main()
{
//2.创建结构体变量:结构体中嵌套结构体
struct teacher t ;
t.id = 1000;
t.name = "老王";
t.age = 50;
t.stu.name = "小小";
t.stu.age = 20;
t.stu.score = 60;
//3.访问结构体变量中的数据
cout << "姓名:" << t.name << "年龄:" <<t.age << "老师的学生名:" << t.stu.name << endl;
}
8.6 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式:
(1)值传递:只可以使用实参,不可以修改实参值
(2)地址传递:可以修改实参的值
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:
//创建学生结构体
struct student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
//2.创建函数
//值传递函数:不能修改实参
void printinfo(struct student stu)
{
cout << "姓名:"<<stu.name << "年龄 " << stu.age << "分数 " << stu.score << endl;
}
//地址传递函数:可以修改实参的值
void printinfo2(struct student *stu)
{
stu->age = 100;
cout <<"姓名:" << stu->name << "年龄 " << stu->age << "分数 " << stu->score << endl;
}
int main()
{
//2.创建结构体变量
struct student s = { "张三",20,88 };
//3.函数调用
//值
printinfo(s);
//地址
printinfo2(&s);
}
8.7 结构体中const使用场景
作用:用const防止误操作
#include <iostream>
using namespace std;
#include <string>
//1.创建结构体:
struct student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
//函数:参数为指针,可以减少内存空间,而且不会复制新的结构体副本。、
//但是传递地址会修改实参的值:为了避免误操作,使用const修饰,函数内将不可以修改
//结构体的值.防止误操作
void printinfo(const struct student* stu)
{
//const修饰结构体,不能修改里面的值
//stu->age = 100;
cout << "姓名:" << stu->name << "年龄 " << stu->age << "分数 " << stu->score << endl;
}
int main()
{
//2.创建结构体变量
struct student s = { "张三",20,88 };
//3.通过函数打印结构体变量的信息
//值
printinfo(&s);
}
8.8 结构体案例
8.8.1 案例 1
案例描述:学校正在做毕业设计项目,每名老师带领5个学生,总共有3名老师,需求如下,设计学生和老师结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。学生的成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师及所带学生赋值。最终打印出老师数据以及老师所带学生的数据。
#include <iostream>
using namespace std;
#include <string>
#include <Ctime>
//案例1:学校正在做毕业设计项目,每名老师带领5个学生,总共有名老师,需求如下
//设计学生和老师结构体,
//其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。
//学生的成员有姓名,考试分数,
//创建数组存放3名老师,通过函数给每个老师及所带学生赋值。
//最终打印出老师数据以及老师所带学生的数据。
//1.创建结构体:
struct student
{
//成员列表
string sName;//姓名
int score;//分数
};
struct teacher
{
string tName;
struct student sArray[5];
};
//赋值函数
void allocateSpace(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() % 60+40;//0~59
tArray[i].sArray[j].score = random;
}
}
}
//打印信息函数
void printname(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 << "学生姓名:" << tArray[i].sArray[j].sName <<
"考试分数:"<< tArray[i].sArray[j].score<<endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//2.创建存放3个老师的数组
struct teacher tArray[3];
//3.通过函数给老师及所带学生赋值
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray,len);
//4.打印信息
printname(tArray,len);
}
8.8.2 案例 2
案例描述:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中 的英雄按照年龄进行升序排列,最终打印排序后的结果。
#include <iostream>
using namespace std;
#include <string>
#include <Ctime>
//案例2:设计一个英雄的结构体,包括成员姓名,年龄,性别;
//创建结构体数组,数组中存放5名英雄。
//通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排列,
//最终打印排序后的结果。
//1.创建结构体:
struct hero
{
//成员列表
string sName;//姓名
int age;//年龄
string gender;//性别
};
//.存放英雄函数
void allocateSpace(hero heroArray[], int len)
{
string names[5] = { "刘备","关羽","张飞","赵云","貂蝉" };
int ages[5] = { 23,22,20,21,19 };
string genders[5] = { "男","男","男","男","女" };
for (int i = 0;i < len;i++)
{
heroArray[i].sName = names[i];
heroArray[i].age = ages[i];
heroArray[i].gender = genders[i];
}
}
//冒泡排序年龄升序排列
void bubblesort(struct hero heroArray[],int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
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].sName << heroArray[i].age<<heroArray[i].gender<<endl;
}
}
int main()
{
//2.创建结构体数组存放五名英雄
struct hero heroArray[5];
int len = sizeof(heroArray) / sizeof(heroArray[0]);
allocateSpace(heroArray, len);
//3.按照年龄排序英雄
bubblesort(heroArray, len);
//4.打印英雄信息
printhero(heroArray, len);
}