结构体定义格式
数据经常以成组的形式存在,这些值能够储存在一起访问起来会简单很多,而如果这些值的结构不同,他们无法储存在同一个数组中。此时,使用结构可以把不同类型的值储存在一起。
聚合数据类型:能够同时存储超过一个的单独数据
1,数组:相同类型的元素的集合
2,结构:可能具有不同类型。
区别:数组元素由于长度相同,可以通过下标来访问;而结构中成员可能长度不同,但是每个成员都有自己的名字,它们是通过名字来访问的。
https://www.bilibili.com/video/BV1Hb411Y7E5?p=65&share_source=copy_web
此图来自视频教学
结构体数组
将自定义的结构体放入数组中方便维护。
# include <iostream>
#include <string>
using namespace std;
//定义结构体
struct member
{
string name;
int age;
int weight;
};
int main()
{
struct member arry[3] =
{
{"hh",16,345},//注意此处用英语中的逗号隔开
{"jj", 23, 24},
{"dd", 34, 43},
};
//数组的遍历
for (int i = 0; i < 3; i++)
{
cout << " name " << arry[i].name << " age "
<< arry[i].age << " weight " << arry[i].weight << endl;
}
system("pause");
return 0;
}
上图为运行结果。
结构体指针
通过指针访问结构体成员
用操作符“->"可以通过结构体指针访问结构体属性。
#include <iostream>
#include <string>
using namespace std;
//定义结构体
struct member
{
string name;
int age;
int weight;
};
int main()
{
//创建结构体变量
struct member hh = { "coraline",13,78 };
//指针指向结构体变量
struct member *p = &hh;
cout << " age: " << p->age << " name: " << p->name << " weight: "
<< p->weight << endl;
system("pause");
return 0;
}
结构体嵌套结构体
顾名思义,在一个结构体中加入另外一个结构体
#include <iostream>
#include <string>
using namespace std;
//注意套如另一个结构体的结构体要写在前面,让电脑阅读。
struct student
{
string name;
string gender;
int age;
};
struct teacher
{
string name;
int year;
struct student stu;
};
int main()
{
//创建结构体变量的另一种方式
struct teacher t;
t.name = "haha";
t.year = 10;
t.stu.age = 18;
t.stu.gender = "girl";
t.stu.name = "coraline";
cout << "teacher's name " << t.name << " teacher's teachering years: " << t.year
<< " teacher's student: " << t.stu.name << " teacher's student's gender: " << t.stu.gender
<< " teacher's student's age: " << t.stu.age << endl;
system("pause");
return 0;
}
结构体做函数参数
把结构体当作实参像函数中传递
#include <iostream>
#include <string>
using namespace std;
//结构体参数设置
struct student
{
string name;
int age;
};
struct teacher
{
string name;
int year;
};
//值传递
void printstudent(struct student h)//注意这里传递的实参是整个结构体
{
cout << "student'name: " << h.name << " student's age: " << h.age << endl;
}
//地址传递(指针+函数)
void printteacher(struct teacher* s)
{
cout << "teacher's name: " << s->name << "teacher's teachering years: " << s->year << endl;
}
int main()
{
//创建结构体变量
struct student h;
h.age = 18;
h.name = "coraline";
printstudent(h);
struct teacher s;
s.name = "princess";
s.year = 28;
printteacher(&s);//注意这里取的是地址(传递地址)
system("pause");
return 0;
}
结构体中const使用场景
防止函数体中的误操作
指针的目的是节省空间
结构体案例
#include <iostream>
#include <string>
#include <Ctime>
using namespace std;
//定义老师与学生的结构体,老师里面包含学生,所以学生在前。
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student stu[5];//有五个学生创建结构数组
};
//创建函数,输入学生老师信息
void allocateteacher(struct teacher tea[], int len)
{
for (int i = 0; i < len; i++)
{
string nameseed = "ABCDE";//区分老师和学生
tea[i].name = "teacher_";
tea[i].name += nameseed[i];
for (int j = 0; j < 5; j++)
{
int random = rand() % 61 + 40;//创建随机数40~100;
tea[i].stu[j].name = "student_";
tea[i].stu[j].name += nameseed[j];
//int random = rand() % 61 + 40;//创建随机数40~100;
tea[i].stu[j].score = random;
}
}
}
//用双重循环来打印或是输入
void printinfor(struct teacher tea[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "teacher's name " << tea[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\tstudent's name " << tea[i].stu[j].name << "student's score" << tea[i].stu->score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//随机种子
struct student stu[5];
struct teacher tea[3];
int len = sizeof (tea ) / sizeof (tea[0]);
allocateteacher(tea, len);
printinfor(tea, len);
system("pause");
return 0;
}
此博客为我在学习视频上做的笔记
视频地址: https://www.bilibili.com/video/BV1Hb411Y7E5?p=71&share_source=copy_web