结构体的定义和使用
#include <iostream>
using namespace std;
//结构体就相当于直接创建的一个数据类型
struct students//结构体相当于是几个数据类型的集合,这的struct不可以省略
{
string name;//名字
int age;//年龄
int score;//分数
}a3;
int main()
{
//通过结构体创建变量的方式有三种:
//1、struct 结构体名 变量名
cout << "第一种:" << endl;
struct students a1;//struct 关键字可以省略
cout << "输入名字:" << endl;
cin >> a1.name;//点“.”来调用结构体中的元素;
cout << "输入年龄:" << endl;
cin >> a1.age;
cout << "输入分数:" << endl;
cin >> a1.score;
cout << "打印刚刚输入的结果:" << endl;
cout << "姓名:" << a1.name << " 年龄:" << a1.age << " 分数:" << a1.score << endl;
//2、struct 结构体名 变量名 = { 成员1值 , 成员2值… }
cout << "第二种:" << endl;
struct students a2 = { "李四",18,80 };
cout << "姓名:" << a2.name << " 年龄:" << a2.age << " 分数:" << a2.score << endl;
//3、定义结构体时顺便创建变量
cout << "第三种:" << endl;
a3.name = "王五";
a3.age = 19;
a3.score = 100;
cout << "姓名:" << a3.name << " 年龄:" << a3.age << " 分数:" << a3.score << endl;
system("pause");
return 0;
}
结构体数组:
#include <iostream>
using namespace std;
//结构体数组
//1、定义结构体
struct stuarr
{
string name;
int age;
int score;
};
int main()
{
//2、创建结构体数组
stuarr arr[3] =
{
{"张三",17,100},
{"李四",18,98},
{"王五",19,60}
};
//3、可以跟数组中的元素重新赋值
arr[2].name = "老八";
arr[2].age=88;
arr[2].score = 99;
//4、打印输出数组信息
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << arr[i].name
<< " 年龄:" << arr[i].age
<< " 分数:" << arr[i].score << endl;
}
system("pause");
return 0;
结构体指针:
#include <iostream>
using namespace std;
struct students
{
string name;//名字
int age;//年龄
int score;//分数
};
int main()
{
struct students a = { "张三",18,100 };
students* p = &a;//通过指针指向结构体变量
cout << "姓名:" << p->name//通过指针来访问结构体变量中的数据
<< " 年龄:" << p->age//指针通过 -> 操作符可以访问成员
<< " 分数:" << p->score << endl;
system("pause");
return 0;
}
结构体嵌套结构体:
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
struct teather//这段上面必须有student的结构体
{
string name;
int id;
int age;
student s;//如果没猜错,这也可以不用写struct
};
int main()
{
teather t;
t.name = "老八";
t.id = 1254088;
t.age = 50;
t.s.name = "张三";
t.s.age = 18;
t.s.score = 100;
cout << "老师姓名:" << t.name << " 老师id:" << t.id << " 老师年龄:" << t.age << endl;
cout << "老师学生:" << t.s.name << " 学生年龄:" << t.s.age << " 学生分数:" << t.s.score << endl;
system("pause");
return 0;
}
结构体做函数的参数:
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
void print01(student s)
{
s.age = 100;
cout << "在子函数01中的打印结果 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;
}
void print02(student *p)
{
p->age = 100;
cout << "在子函数02中的打印结果 姓名:" << p->name
<< " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main()
{
student s;
s.name = "Jam";
s.age = 18;
s.score = 85;
print01(s);//值传递,不会改变实参;
cout << "在main函数中的打印结果 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;//结果和main函数中的数据一样
print02(&s);//地址传递,可以改变实参
cout << "在main函数中的打印结果 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;//main函数的打印结果被子函数02改变;
system("pause"); //变得跟子函数一样的结果
return 0;
}
结构体中cons的使用场景:
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
void print01(const student* s)//值传递占用空间较大,改为地址传递
{
//s->age = 100;
//但是我并不想改变main函数中的数据,这时候就加const,加了之后这就要报错,防止误操作
cout << "在子函数01中的打印结果 姓名:" << s->name
<< " 年龄:" << s->age << " 分数:" << s->score << endl;
}
int main()
{
student s;
s.name = "Jam";
s.age = 18;
s.score = 85;
print01(&s);
system("pause");
return 0;
}
结构体案例一:
案例描述:
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
最终打印出老师数据以及老师所带的学生数据。
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
//创建学生的结构体
struct student
{
string sname;
int score;
};
//创建一个老师的结构体:
struct teacher
{
string tname;
student sarr[5];//每个老师5个学生
};
void allocatespace(struct teacher tarr[], int len)//值传递
{
string namespeed = "ABCDE";
for (int i = 0; i < 3; i++)
{
tarr[i].tname= "老师_";
tarr[i].tname += namespeed[i];
for (int j = 0; j < 5; j++)
{
int random = rand() % 61 + 40;
tarr[i].sarr[j].sname = "学生_";
tarr[i].sarr[j].sname += namespeed[j];
tarr[i].sarr[j].score = random;
}
}
}
void print(struct teacher tarr[],int len)
{
for (int i=0; i < 3; i++)
{
cout << "老师姓名:" << tarr[i].tname << endl;
for (int j=0;j<5;j++)
{
cout << "\t学生姓名:" << tarr[i].sarr[j].sname
<< " 考试分数:" << tarr[i].sarr[j].score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//随机数种子,放在main函数中
teacher tarr[3];//创建老师的数组
int len = sizeof(tarr) / sizeof(tarr[0]);//数组长度
allocatespace(tarr, len);//赋值函数,老师学生通通赋值;
print(tarr, len);//打印函数
system("pause");
return 0;
}
结构体案例二:
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
#include <iostream>
using namespace std;
struct Hero
{
string name;
int age;
string sex;
};
void bubble(struct Hero H[],int len)
{
for (int i = 0; i < len-1;i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (H[j].age > H[j + 1].age)
{
Hero temp = H[j];
H[j] = H[j + 1];
H[j + 1] = temp;
}
}
}
}
void print(struct Hero H[], int len)
{
cout << "排序后的结果:" << endl;
for (int i = 0; i < 5; i++)
{
cout << "英雄名:" << H[i].name
<< " 年龄:" << H[i].age << " 性别:"
<< H[i].sex << endl;
}
}
int main()
{
struct Hero H[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
cout << "未排序前的结果:" << endl;
for (int i = 0; i < 5; i++)
{
cout << "英雄名:" << H[i].name
<< " 年龄:" << H[i].age << " 性别:"
<< H[i].sex << endl;
}
int len = sizeof(H) / sizeof(H[0]);
bubble(H, len);//要排序,要把数组给他
print(H, len);
system("pause");
return 0;
}