1.结构体基本概念
概念:
结构体属于用户自定义的数据类型,允许用户自定义的数据类型
结构体定义和使用:
语法:struct 结构体名称 { 结构体成员列表};
创建方式3种:
(1)创建学生数据类型:学生(姓名、年龄、分数)
//自定义数据类型,一些类型的集合组成的数据类型
//语法: struct 类型名称 {成员列表}
//此处struct关键字不可以省略
struct Student
{
//成员列表
string name;
int age;
int score;
};//注意不能忘记分号
(2)通过学生类型创建具体学生//此处struct关键字可以省略
//2.1 struct Student s1
struct Student s1;
s1.name="zhangsan";
s1.age=19;
s1.score=90;
cout<<"name:"<<s1.name<<"age:"<<s1.age<<"grade:"<<s1.score<<endl;//注意添加#include<string>,否则第三个<<会报错
//2.2 struct Student s2={....}
struct Student s2={"lisi",18,70};
cout<<"name:"<<s2.name<<"age:"<<s2.age<<"grade:"<<s2.score<<endl;
(3)定义结构体时顺便定义结构体变量
struct Student
{
//成员列表
string name;
int age;
int score;
}s3;
2.结构体数组
语法:struct 结构体名 数组名[元素个数]={ {},{},{}....}
步骤
1)定义结构体
2)主函数中创建结构体数组
3)给结构体数组中的元素赋值
4)遍历结构体数组
#include<iostream>
using namespace std;
#include<string>
//1.定义结构体
struct Student
{
string name;
int age;
int score;
};
int main()
{
//2.创建结构体数组
struct Student stuArray[3]=
{
{"zhangsan",18,100},{"lisi",19.80},{"wangwu",20,70}
};
//3.给结构体数组中的元素赋值
stuArray[2].name="zhaoliu";//修改
stuArray[2].age=13;
stuArray[2].score=60;
//4.遍历结构体数组
for(int i=0;i<3;i++)
{
cout<<"name:"<<stuArray[i].name<<"age:"<<stuArray[i].name<<"score:"<<stuArray[i].score<<endl;
}
system("pause");
return 0;
}
3结构体指针
用操作符->访问结构体属性
步骤:
创建学生结构体变量
通过指针指向结构体变量
通过指针访问结构体变量中的数据
#include<iostream>
using namespace std;
#include<string>
//结构体指针
struct Student
{
string name;
int age;
int score;
};
int main()
{
//创建学生结构体变量
struct Student s={"zhangsan",18,100};//struct可省略
//通过指针指向结构体变量
struct Student *p=&s;//s是结构体变量为Student类型,因此指针为Student类型,struct可省略
//通过指针访问结构体变量中的数据
cout<<"name:"<<p->name<<"age:"<<p->age<<"score:"<<p->score;
system("pause");
return 0;
}
4结构体套用
利用一个结构体作为另外一个结构体的成员
#include<cstdio>
using namespace;
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=1230;
t.name="laowang";
t.age=50;
t.stu.age=18;
t.stu.name="xiaowang";
t.stu.score=100;
cout<<"teacher name:"<<t.name<<"teacher id:"<<t.id<<"teacher age:"<<t.age<<"stu:"<<t.stu.name<<endl;
system("pause");
return 0;
}
5结构体做函数参数
结构体做函数参数
传递方式:1.值传递,2.地址传递
#include<iostream>
#include<string>
using namespace std;
//定义学生结构体
struct student
{
string name;
int age;
int score;
};
//打印学生信息函数
//1.值传递
void printStudent1(struct student s)
{
//s.age=100;
cout<<"子函数1 姓名:"<<s.name<<"age:"<<s.age<<"score:"<<s.score<<endl;
}
//2.地址传递
void printStudent2(struct student*p)
{
cout<<"子函数2 姓名:"<<p->name<<"age:"<<p->age<<"score:"<<p->score<<endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生的信息
//创建结构体变量
struct student s;
s.age=18;
s.name="mike";
s.score=100;
//printStudent1(s);//值传递,在printstudent1中可以修改形参数据,main中不变
printStudent2(&s);//地址传递,在printstudent2中可以修改形参数据,main也改变
//cout<<"main name:"<<s.name<<"age:"<<s.age<<"grade:"<<s.score<<endl;
system("pause");
return 0;
}
6结构体中const使用场景
用const来防止误操作
#include<iostream>
#include<string>
using namespace std;
//const使用场景
//定义学生结构体
struct student
{
string name;
int age;
int score;
};
void printStudent(student s)//const student*p 将函数中的形参改为指针可以减少内存,且不会有新的副本
{
//s->age=150//加入const之后报错;加入const之后不可修改值,可以防止误操作
cout<<"name:"<<s.name<<"age:"<<s.age<<"score:"<<s.score<<endl;
}
int main()
{
//创建结构体变量
struct student s={"张三",15,90};
//通过函数打印结构体变量
printStudent(s);
//printStudent(&s);
system("pause");
return 0;
}
案例一:
*学校正在做毕设项目,每名老师带领5个同学,总共有3名老师。要求如下:
设计学生和老师的结构体,
其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放3名老师,
通过函数给每个老师及所带的学生赋值,最终打印数据所带的学生数据。
*/
#include<iostream>
#include<string>
using namespace std;
#include<ctime>
//学生结构体
struct Student
{
string sname;
int score;
};
//老师结构体
struct Teacher
{
string tname;
//学生数组
struct Student sArray[5];
};
void allocateSpace(struct Teacher tArray[5],int len)
{
string nameSeed="ABCDE";//类似于通配符的用法
//给老师赋值
for(int i=0;i<len;i++){
tArray[i].tname="Teacher_";
tArray[i].tname+=nameSeed[i];//追加nameSeed中的位置
//通过循环给每名老师所带的学生赋值
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 +40 --> 40~99 ;%60改为%61 --》40~100
tArray[i].sArray[j].score=random;//随机
}
}
}
void printInfo(struct Teacher tArray[],int len)
{
for(int i=0;i<len;i++)
{
cout<<"teacher name:"<<tArray[i].tname<<endl;
for(int j=0;j<5;j++)
{
cout<<"\tstudent name:"<<tArray[i].sArray[j].sname<<"score:"<<tArray[i].sArray[j].score<<endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//创建3名老师的数组
struct Teacher tArray[3];
//通过函数给三名老师的信息赋值,并给老师所带的学生的信息赋值
int len=sizeof(tArray)/sizeof(tArray[0]);
allocateSpace(tArray,len);
//打印所有老师及所带的学生信息
printInfo(tArray,len);
system("pause");
return 0;
}
案例二:
//设计一个英雄结构体,包括姓名、年龄、性别;创建结构体数组,数组中存放五名英雄,
//通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,所以中打印排序后的结果。
#include<iostream>
#include<string>
using namespace std;
//1.设计英雄结构体
struct Hero
{
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<<"name:"<<heroArray[i].name<<"age:"<<heroArray[i].age<<"sex:"<<heroArray[i].sex<<endl;
}
}
int main()
{
//2.创建数组存放5名英雄
struct Hero heroArray[5]=
{
{"liubei",23,"nan"},
{"guanyu",22,"nan"},
{"zhangfei",20,"nan"},
{"zhaoyun",21,"nan"},
{"diaochan",19,"nv"}
};
//测试代码
int len=sizeof(heroArray)/sizeof(heroArray[0]);
//for(int i=0;i<len;i++){
// cout<<"name:"<<heroArray[i].name<<"age:"<<heroArray[i].age<<"sex:"<<heroArray[i].sex<<endl;
//}
//3.对数组进行排序(按照年龄)
bubbleSort(heroArray, len);
//4.将排序后的结果打印输出
printHero(heroArray,len);
system("pause");
return 0;
}