结构体
结构体:用户自定义的数据类型,允许用户存储不同的数据类型。
一、结构体的定义和使用
语法:struct 结构体名 {结构体成员列表}
通过 结构体创建 变量的方式有三种
(1)//通过给S1属性赋值,访问结构体变量中的属性
(2) struct Student s2={…}
(3)在定义结构体时顺便创建结构体变量
一般来说 方式1和2更为常见.
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
//1.创建 学生的数据类型
struct Student//结构体定义的时候,关键字struct不可省
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;//方式3顺便创建结构体变量
//2.学生类型创建具体学生
int main()
{
//struct关键字可以省
struct Student s1;//通过给S1属性赋值,访问结构体变量中的属性
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名是:" << s1.name<<" 年龄是:"<<s1.age << " 分数为:"<<s1.score << endl;
// 2.2 struct Student s2={....}
struct Student s2 = { "李四",19,80 };
cout << "姓名是:" << s2.name << " 年龄是:" << s2.age << " 分数为:" << s2.score << endl;
// 2.3 在定义结构体时顺便创建结构体变量
s3.name = "王五";
s3.age = 13;
s3.score = 60;
cout << "姓名是:" << s3.name << " 年龄是:" << s3.age << " 分数为:" << s3.score << endl;
system("pause");
return 0;
}
注意
1.在定义结构体时顺便创建结构体变量
2. 创建结构体变量的时候,struct关键字可以省
3. 结构体变量利用操作符. 来进行访问成员
二、结构体数组
将自定义的结构体放入到 数组中方便维护
struct 结构体 数组名
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
//1.定义 结构体
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//2创建 结构体
struct Student stuArray[3] =
{
{"张三",18,100},
{ "李四", 19, 80 },
{"王五",18,100},
};
//3给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
//4遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名" << stuArray[i].name
<< "年龄" << stuArray[i].age
<< "分数" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
三、结构体指针
通过指针访问结构体中的成员
利用 -> 可以访问结构体中的元素
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
//1. 结构体指针
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//2创建 结构体变量
struct Student s = { "张三",18,100 };
// { "李四", 19, 80 },
// {"王五",18,100},
//3通过指针指向结构体变量;
Student * p = &s;
//4通过结构体指针访问 结构体中的属性 使用->
cout << "姓名" << p->name<< "年龄" << p->age<< "分数" << p->score << endl;
system("pause");
return 0;
}
四、结构体嵌套结构体
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
//定义学生的
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//定义老师的结构体
struct Teacher
{
int id;
int age;
string name;
struct Student stu;//辅导的学生
};
int main()
{
//2创建 结构体变量
Teacher t;
t.id = 1000;
t.name = "老王";
t.age = 19;
//结构体嵌套结构体的时候如何定义
t.stu.name = "张三";
t.stu.age = 10;
t.stu.score = 60;
cout << "老师的姓名: " << t.name << "老师编号 " << t.id << "老师年龄 " << t.age << endl;
cout << "老师辅导的学生的姓名:" << t.stu.name << "学生年龄 " << t.stu.age
<< "学生分数 " << t.stu.score << endl;
system("pause");
return 0;
}
五、结构体做函数参数
func:将结构体作为参数向函数中传递
传递方式2种:
1.值传递
2.地址传递
最为 重要的是::如果不想要改变主函数的数据,使用值传递。反之,使用 地址传递。
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
//定义学生的
struct student//定义结构体
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//值传递
void printStudent1(struct student s)
{
s.age = 100;
cout << "子函数1中的 姓名" << s.name << "年龄 " << s.age << "分数 " << s.score << endl;
}
//地址传递
void printStudent2(struct student *p)
{
p->age = 200;
cout << "子函数2中的 姓名" << p->name << "年龄 " << p->age << "分数 " << p->score << endl;
}
int main()
{
//2创建 结构体变量
struct student s;
s.name = "张三";
s.age = 10;
s.score = 60;
printStudent1(s);
printStudent2(&s);
cout << "main2中的 姓名" << s.name << "年龄 " << s.age << "分数 " << s.score << endl;
system("pause");
return 0;
}
六、结构体const使用场景
使用const 来防止误操作
#include<iostream>
using namespace std;
#include<string>;//输出字符串的时候,需要加上 string 头文件
struct student//定义结构体
{
//姓名 年龄 //分数
string name;
int age;
int score;
};
//将函数中的形参改为指针,可以减少内存空间 指针只占4个字节
void printStudent(const student *s)
{
//s->age = 10;//加入const 之后,一旦 有修改 的操作,就会报错,可以防止误操作
cout << "姓名" << s->name << "年龄 " << s->age << "分数 " << s->score << endl;
}
int main()
{
//2创建 结构体变量
struct student s = { "张三" ,10 ,60};
printStudent(&s);
cout << "main中的 姓名" << s.name << "年龄 " << s.age << "分数 " << s.score << endl;
system("pause");
return 0;
}
七、结构体案例
每名老师带5个学生,3个老师
1.设计学生和老师的结构体
2.老师的结构体中,老师姓名 5个学生的数组作为成员
3.学生的有 姓名 考试分数,创建数组存放 3名老师,通过函数 给每个老师以及学生赋值
4,打印老师数据以及老师 所带的学生数据