8结构体
8.1结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
8.2结构体定义和使用
语法:struct 结构体名{结构体成员列表};
通过结构体创建变量的方式有三种:
struct student
{string name;int age;int score;}s3;//s3为结构体变量
1)struct 结构体名 变量名
struct student s1
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<"分数"<<s1.score<<endl;
2)struct 结构体名 变量名={成员1值,成员2值…}
struct student s2={"李四",19,80};
cout<<"姓名"<<s2.name<<"年龄"<<s2.age<<"分数"<<s2.score<<endl;
3)定义结构体时顺便创建变量
s3.name = "王五";
s3,age = 20;
s3.score = 60;
cout<<"姓名"<<s3.name<<"年龄"<<s3.age<<"分数"<<s3.score<<endl;
注:结构体创建变量时,struct关键字可以省略,结构体定义时不可以省略
8.3结构体数组
作用:将自定义的结构体放入数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {},{},。。。{} }
//1、定义一个结构体
struct student
{string name;int age;int score;};
int main
{
//2、创建结构体数组
struct student stuarray[3] =
{
{"张三",18,100},
{"李四",19,90},
{"王五",20,80}
};
//3、给结构体数组中的元素赋值
stuarray[2].name = "赵六";//将王五改为赵六
//4、遍历结构体数组
for(int i=0;i<3;i++)
{
cout<<"姓名"<<stuarray[i].name<<"年龄"<<stuarray[i].age<<"成绩"<<stuarray[i].score<<endl;
}
8.4结构体指针
作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
struct student
{string name;int age;int score;};
int main()
{
//1、创建学生结构体变量
struct student s = {"张三",18,100};//struct可省略
//2、通过指针指向结构体变量
struct student *p = &s;//struct可省略
//3、通过指针访问结构体变量中的数据
//p->访问属性
cout<<"姓名"<<p->name<<"年龄"<<p->age<<"成绩"<<p->score<<endl;
8.5结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
eg:每个老师辅导一个学员,一个老师结构体中,记录一个学生的结构体
struct student
{string name;int age;int score;};
struct teacher
{ int id;string name;age;struct student stu};
int main()
{
teacher t;
t.id=1000;
t.name="wang";
t.age=50;
t.stu.age=20;
t.stu.name="li";
t.stu.score=90;
总结:在结构体中可以定义另一个结构体作为成员,用了解决实际问题
8.6结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式:
1)值传递;
2)地址传递;
struct student
{
string name;
int age;
int score;
};
//打印学生信息的函数
//1、值传递
void printstudent1(struct student s)
{
cout << "子函数1中打印姓名" << s.name << "年龄" << s.age << "分数" << s.score << endl;
}
//2、地址传递
void printstudent2(struct student* p)
{
cout << "子函数2中打印姓名" << p->name << "年龄" << p->age << "分数" << p->score << endl;
}
int main()
{
student s;
s.name = "张三";
s.age = 20;
s.score = 90;
printstudent1(s);
printstudent2(&s);
cout << "main函数中打印姓名" << s.name << "年龄" << s.age << "分数" << s.score << endl;
system("pause");
return 0;
}
注:指针访问结构体属性时需要用->,例:p->name;
值传递时,形参改变,不会改变实参;
地址传递时,形参改变,实参也会改变
总结:如果不想修改主函数中的数据(实参),用值传递,反之用地址传递
8.7结构体中const使用场景
作用:用const来防止误操作
struct student
{
string name;
int age;
int score;
};
void printstudent(const student* s)
{
cout << "姓名 " << s->name << " 年龄 " << s->age << " 分数 " << s->score << endl;
}
int main()
{
student s = { "张三" ,20,90};
printstudent(&s);
cout << "main函数中打印姓名 " << s.name << " 年龄 " << s.age << " 分数 " << s.score << endl;
system("pause");
return 0;
}
8.8结构体案例
8.8.1案例1
8.8.2案例2
#include <iostream>
using namespace std;
#include <string>
struct hero
{
string name;
int age;
string gender;
};
//冒泡排序升序函数
void paixu(struct hero harray[],int len)
{
for (int i = 0; i < len -1; i++)
{
for (int j = 0; j < len - i-1 ; j++)
{
if (harray[j].age > harray[j+ 1].age)
{
struct hero temp = harray[j];//创建临时英雄
harray[j] = harray[j + 1];
harray[j + 1] = temp;
}
}
}
}
//打印输出函数
void printhero(struct hero harray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "英雄姓名 " << harray[i].name << " 年龄 " << harray[i].age << " 性别 " << harray[i].gender << endl;
}
}
int main()
{
//创建五个英雄数组数组
struct hero harray[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",21,"男"},
{"赵云",20,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(harray) / sizeof(harray[0]);
paixu(harray, len);
printhero(harray, len);
system("pause");
return 0;
}