前言
前面讲了指针的作用
结构体定义
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:
struct 结构体名 变量
struct 结构体名 变量 = {成员1值,成员2值...}
定义结构体时顺创建变量
#include<iostream>
#include<string>
using namespace std;
//1、创建学生数据类型 :学生包括(姓名,年龄,分数)
//语法 struct 结构体名 {成员}
struct Student
{
//成员列表
//姓名
String name;
//年龄
int age;
//分数
int score;
}s3;//顺便创建一个结构体变量
//2、通过学生类创建具体的学生
int main()
{
//2.1 struct Student s1
struct Student s1 // 创建变量时,关键字struct可以省略,但是定义的时候不可以省略
s1.name ="张三";
s1.age = 18;
s1.score = 150;
cout << "姓名:"<< s1.name << "年龄:"<< s1.age <<"分数:"<< s1.score << endl;
//2.2 struct Student s2 = { ... }
struct Student s2 = {"李四", 19, 150};
cout << "姓名:"<< s2.name << "年龄:"<< s2.age <<"分数:"<< s2.score << endl;
//2.3 在定义结构体时顺便创建结构体变量
s3.name = "王五";
s3.age = 20;
s3.score = 150;
cout << "姓名:"<< s3.name << "年龄:"<< s3.age <<"分数:"<< s3.score << endl;
system("pasue");
return 0;
}
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数 ] = {{},{},{},... {}}
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}
}
int main()
{
//2、创建结构体数组
struct Student stuArray[3] = { {"张三",18,150},{"李四",20,150},{"王五",19,150}};
//3、给结构体数组中的元素赋值
stuArray[2].name = "赵六"; //第2个人的名字改为赵六
//4、遍历结构体数组
for(int i = 0;i < 3; i++)
{
cout << "姓名:" << stuArray[i].name
<< "年龄:" << stuArray[i].age
<< "分数:" << stuArray[i].score << endl;
}
system("pasue");
return 0;
}
结构体指针
作用:通过指针访问结构体的成员
利用操作符- >可以通过结构体指针访问结构体属性
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student
{
string name;
int age;
int score;
}
int main()
{
//2、创建结构体变量
struct Student s = { "张三",18,150 }; //struct可以省略
//3、通过指针指向结构体变量
struct Student *p =&s; //struct可以省略
//4、通过指针访问结构体变量中的数据
cout << "姓名:"<<p->name << "年龄:"<< p->age << "分数:" << p->score << endl;
system("pasue");
return 0;
}
结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#include<iostream>
#include<string>
using namespace std;
//1、定义学生结构体
struct Student
{
string name;
int age;
int score;
}
//2、定义老师结构体
struct Teacher
{
int id; //教师编号
string name; //教师姓名
int age; //教师年龄
struct Student stu; //辅导的学生,必须在定义老师结构提之前定义学生结构体
int main()
{
//2、创建嵌套结构体
//创建老师结构体变量
struct Teacher t;
t.id = 123456;
t.name = "王老师";
t.age = 40;
t.stu.name ="张三";
t.stu.age = 18;
t.stu.score = 150;
cout << "老师姓名:"<<t.name << "老师的年龄:"<< t.age << "学生的分数:" << t.stu.score << endl;
system("pasue");
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
1、值传递
2、地址传递
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct Student
{
string name;
int age;
int score;
}
//值传递
void printStudent1(struct Student s)
{
cout << "1姓名:"<< s.name << "1年龄:"<< s.age << "1分数:" << s.score << endl;
}
//地址传递
void printStudent2(struct Student *p)
{
cout << "2姓名:"<< p->name << "2年龄:"<< p->age << "2分数:" << p->score << endl;
}
int main()
{
//结构体做函数参数
//将学生传入一个参数中,打印学生身上的所有信息
//创建结构体变量
struct Student s;
s.name = "王";
s.age = 18;
s.score = 150;
printStudent1(s); //值传递
printStudent2(&s); //地址传递
system("pasue");
return 0;
}
结构体中const使用场景
作用:用const 来防止误操作
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct Student
{
string name;
int age;
int score;
}
//值传递
void printStudent(struct Student s)
{
cout << "姓名:"<< s.name << "年龄:"<< s.age << "分数:" << s.score << endl;
}
//地址传递
void printStudent(const Student *p)
{
p->age = 150; //当加一个const后,此时这里就会出错,原因是不能进行修改,只能读 不能写
cout << "姓名:"<< s.name << "年龄:"<< s.age << "分数:" << s.score << endl;
}
int main()
{
//创建结构体变量
struct Student s = {"张三",15,150};
//通过打印结构体变量信息
printStudent(s); //值传递
printStudent(&s); //地址传递
system("pasue");
return 0;
}
结构体练习案例1
案例描述:每名老师带5个学生,共有3名老师,需求如下:设计学生何老师结构体,其中在老师的结构体中,有老师和一个存放5名学生的数组成员,学生的成员有姓名、考试分数、创建数组存放3名老师,通过函数给每个老师以及带的学生赋值,最终打印出老师数据以及老师所带的学生数据
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//定义学生结构体
struct Student
{
string name;
int age;
int score;
}
//定义教师结构体
struct Teacher{
string Tname;
struct student stuArray[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].tArray[j].name ="student_"
tArray[i].tArray[j].name +=nameSeed[j];
int random = rand ()%60+40 //40~99
tArray[i].tArray[j].score =random;
}
}
}
void Info(struct Teacher tArray[], int len)
{
for(int i = 0; i < len; i++)
{
cout <<"老师的名字:"<< tArray[i].name << endl;
for(int j = 0; j < len; j++)
{
cout <<"\t学生的名字:"<< tArray[i].tArray[j].name
<<"学生的成绩:"<< tArray[i].tArray[j].score << endl;
}
}
}
int main()
{
//随机数种子 为了打印的成绩随机
srand((unsigned int)time(NULL));
//创建3名老师的数组
struct Student s = {"张三",15,150};
//通过函数给3名老师的信息赋值,并给老师带的学生进行赋值
int len =sizeof (tArray)/sizeof (tArray[0])
allocateSpace(tArray,len)
system("pasue");
return 0;
}
结构体练习案例2
案例描述:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
5名英雄信息如下:
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"}
{"赵云",21,"男"}
{"貂蝉",19,"女"}
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//定义英雄结构体
struct Hero
{
string name;
int age;
string sex;
}
//冒泡排序
bubbleSort(struct Hero heroArray[], int len)
{
for(int i = 0; i < len-1; i++)
{
for(int j = 0; j < len-i-1; j++)
{
if(heroArray[j].age >heroArray[j+1].age)
{
struct Hero temp = heroArray[j].age;
heroArray[j].age =heroArray[j+1].age;
heroArray[j+1].age = temp;
}
}
}
void printInfo(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()
{
//创建英雄的数组
struct Hero heroArray[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}};
//打印排序前的
int len =sizeof(heroArray) / sizeof(heroArray[0])
for(int j = 0; j < len; j++)
{
cout <<"姓名:"<< heroArray[i].name
<<"年龄:"<< heroArray[j].age
<<"性别:"<< heroArray[j].sex
<< endl;
}
//对数组进行排序,按年龄升序排序
bubbleSort(heroArray,len);
//打印排序后的
printInfo(heroArray,len);
system("pasue");
return 0;
}