结构体——结构体的定义和使用
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:struct 结构体名{结构体成员列表}
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
int main()
{
//定义变量方法1
struct student s1;
//定义变量方法2
struct student s2 = { "张三",18,100 };
s1.age = 19;
s1.name = "李四";
s1.score = 100;
cout << s1.name << s1.age << s1.score << endl;
cout << s2.name << s2.age << s2.score << endl;
return 0;
}
定义结构体时关键字struct不可省略
定义结构体变量时关键字struct可以省略
结构体变量利用 . 访问成员
结构体数组
将自定义的结构体放入数组中方便维护
语法:struct 结构体 数组名[元素个数]={{},{},{}};
步骤:
- 定义一个结构体
- 创建一个结构体数组
- 给结构体数组中元素赋值
- 遍历结构体数组
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student std[3] = { {"张三",18,80},{"李四",19,90}, {"王五",20,100} };
for (int i = 0; i < 3; i++)
{
cout << std[i].name << " " << std[i].age << " " << std[i].score << endl;
}
return 0;
}
结构体指针
作用通过指针访问结构体中的成员
利用->可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student std[3] = { {"张三",18,80},{"李四",19,90}, {"王五",20,100} };
//结构体名称本质上就是一个数据类型
//数组名称就是首地址
struct student* p = std;
for (int i = 0; i < 3; i++)
{
cout << (p + i)->name << (p + i)->age << (p + i)->score << endl;
}
return 0;
}
结构体嵌套结构体
作用:结构体中的成员可以时另一个结构体
需求:每个老师辅导三个学生,记录三个学生的结构体
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
struct teacher
{
string name;
int age;
student std[3];
};
int main()
{
struct teacher t1 = { "李老师",50,{{"张三",18,80},{"李四",19,90},{"王五",20,100}} };
for (int i = 0; i < 3; i++)
{
cout << "指导老师 = " << t1.name << " "<< "老师年龄 = " << t1.age << " " << "指导学生 = " << t1.std[i].name << " " << t1.std[i].age << " " << t1.std[i].score << endl;
}
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数传递
传递方式:
- 值传递——实参不变
- 地址传递——实参变化
需求案例:创建一个函数,打印结构体,用值传递和地址传递分别实现
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
//这里利用了函数重载,后面的知识会介绍,注意指针用->
void print(struct student s)
{
cout << s.name << " " << s.age << " " << s.score << endl;
}
void print(struct student* s)
{
cout << s->name << " " << s->age << " " << s->score << endl;
}
int main()
{
struct student s1 = { "张三",18,100 };
//注意定义指针指向的时地址内存空间,函数地址传递传的也是地址内存空间
student* p = &s1;
print(s1);
print(p);
return 0;
}
看打印是在主函数打印还是函数体内打印决定是使用值传递还是地址传递
函数重载在日常运用中及其好用但要注意格式统一(后续介绍)
结构体中const的使用场景
作用:用const防止误操作,减少数据量
我们之前谈到地址传递可以减少内存的使用(不用新开辟内存),但是又面临的实参的值会被改变,那么用const就可以实现既减少内存的使用,又不改变实参
#include <iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
//在形参前面加const便可防止实参被改变
//地址传递减少内存使用,只占4个字节
void printstudent(const struct student*s)
{
cout << "学生姓名" << " " << s->name << " " << "学生年龄" << " " << s->age << " " << "学生成绩" << " " << s->score << endl;
}
int main()
{
struct student s1 = { "张三",18,100 };
student* p = &s1;
printstudent(p);
return 0;
}
结构体案例
需求案例:每名老师带五名学生,总共三名老师
具体需求:
老师的结构体中,有老师的姓名和一个存放5名学生的结构体数组
学生结构体中,有姓名,考试分数
创建数组存放3个老师,通过函数给老师和学生赋值,并且打印出来
#include <iostream>
using namespace std;
#include<string>
//创建学生结构体
struct student
{
string name;
int score;
};
//创建老师结构体
struct teacher
{
string name;
//创建学生结构体数组
student std[5];
};
//创建添加老师学生的函数
void add(struct teacher t[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "请输入第" << i + 1 << "个老师的姓名" << endl;
//输入老师名字
string name1;
cin >> name1;
t[i].name = name1;
cout << "请输入" << t[i].name << "带多少个学生" << endl;
//输入老师带的学生数量并且将此变量作为后续循环输入学生信息的循环节
int count;
cin >> count;
for (int j = 0; j < count; j++)
{
cout << "请输入" << t[i].name << "带的第" << j + 1 << "个学生的信息" << endl;
cout << "姓名" << endl;
string name2;
cin >> name2;
//层层访问注意【】内的是i还是j
t[i].std[j].name = name2;
cout << "分数" << endl;
int score1;
cin >> score1;
t[i].std[j].score = score1;
}
//一个老师输入完清空一次屏幕有利于用户体验
system("pause");
system("cls");
}
}
void print(struct teacher t[], int len)
{
for (int i = 0; i < len; i++)
{
//定义一个变量计算每一个老师所带的学生数量并作为后续循环输出的循环节
int num = sizeof(t[i].std) / sizeof(t[i].std[0]);
cout << "第" << i + 1 << "指导老师" << t[i].name << "=" << endl;
for (int j = 0; j < num; j++)
{
cout<<"学生姓名" << " " << t[i].std[j].name << "学生分数" << " " << t[i].std[j].score << endl;
}
}
}
int main()
{
struct teacher t[3];
//通过公式计算数组长度便于规范通用化
int len = sizeof(t) / sizeof(t[0]);
add(t, len);
print(t, len);
return 0;
}