结构体
结构体属于用户 == 自定义的数据类型 == 允许用户存储不同的数据类型
结构体定义与使用
语法:struct
结构体名 { 结构体成员列表 }
通过结构体创建变量的方式有三种:
-
struct 结构体名 变量名
-
struct 结构体名 变量名 = {成员1值,成员2值...}
-
定义结构体时顺便创建变量
【demo】:
#include <iostream>
#include <string>
using namespace std;
// 结构体定义
struct student
{
string name; // 姓名
string sex; // 性别
int age; // 年龄
int score; // 分数
} stu3; // 结构体变量创建方式3
int main()
{
// 结构体变量创建方式1
struct student stu1;
stu1.name = "张三";
stu1.age = 20;
stu1.sex = "男";
stu1.score = 90;
cout << "姓名:" << stu1.name << ",年龄:" << stu1.age << ",性别:" << stu1.sex << ",分数:" << stu1.score << endl;
// 结构体变量创建方式2
struct student stu2 =
{
"李四",
"男",
20,
85};
cout << "姓名:" << stu2.name << ",年龄:" << stu2.age << ",性别:" << stu2.sex << ",分数:" << stu2.score << endl;
// 创建方式3
stu3.name = "王五";
stu3.sex = "女";
stu3.age = 18;
stu3.score = 95;
cout << "姓名:" << stu3.name << ",年龄:" << stu3.age << ",性别:" << stu3.sex << ",分数:" << stu3.score << endl;
return 0;
}
小结
- 定义结构体的关键字是
struct
,不能省略 - 创建结构体变量时,关键字
struct
可以省略不写 - 结构体变量使用操作符
"."
来访问成员变量
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[元素个数] = {
{},
{},
{},
...
}
【demo】:
#include <iostream>
#include <string>
using namespace std;
struct student
{
//成员变量
string name;
string gender;
int age;
};
int main()
{
struct student stu[3] = {
{"张三", "男", 20},
{"李四", "男", 21},
{"王五", "女", 18}};
int len = sizeof(stu) / sizeof(stu[0]);
for (int i = 0; i < len; i++)
{
cout << "姓名: " << stu[i].name << " , 性别: " << stu[i].gender << " , 年龄: " << stu[i].age << endl;
}
return 0;
}
结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符 -> 可以通过结构体指针访问结构体属性
#include <iostream>
#include <string>
using namespace std;
// 定义结构体
struct student
{
// 成员列表
string name;
string gender;
int age;
};
int main()
{
struct student stu =
{
"张三",
"男",
20};
struct student *p = &stu;
cout << "姓名:" << stu.name << " 性别:" << stu.gender << " 年龄:" << stu.age << endl;
p->age = 22;
cout << "姓名:" << p->name << " 性别:" << p->gender << " 年龄:" << p->age << endl;
return 0;
}
小结:结构体指针可以通过 -> 操作符 来访问结构体中的成员变量
结构体中嵌套结构体
作用:结构体中的成员可以是另一个结构体
#include <iostream>
#include <string>
using namespace std;
// 学生
struct student
{
string name;
string gender;
int age;
};
// 教师
struct teacher
{
int id;
string name;
int age;
struct student stu; // 子结构体 学生
};
int main()
{
struct teacher t1;
t1.id = 1;
t1.name = "老孙";
t1.age = 39;
struct student s1 =
{"张三", "男", 20};
t1.stu = s1;
cout << "教职工编号:" << t1.id << " 姓名:" << t1.name << " 年龄:" << t1.age << " 学生:" << t1.stu.name << endl;
return 0;
}
小结:在结构体中可以定义另一个结构体作为成员,用来解决实际问题
结构体作为函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
- 值传递
- 地址传递
#include <iostream>
#include <string>
using namespace std;
// 定义结构体
struct student
{
// 成员列表
string name;
string gender;
int age;
};
// 值传递
void printStudent(student stu)
{
stu.age = 30;
cout << "值传递:"
<< "name:" << stu.name << ",gender:" << stu.gender << ",age:" << stu.age << endl;
}
void printStudent2(student *stu)
{
stu->age = 18;
cout << "地址传递:"
<< "name:" << stu->name << ",gender:" << stu->gender << ",age:" << stu->age << endl;
}
int main()
{
struct student stu =
{"张三", "男", 20};
printStudent(stu);
cout << "main函数中:"
<< "name:" << stu.name << ",gender:" << stu.gender << ",age:" << stu.age << endl;
cout << endl;
printStudent2(&stu);
cout << "main函数中:"
<< "name:" << stu.name << ",gender:" << stu.gender << ",age:" << stu.age << endl;
return 0;
}
小结:如果不想修改主函数中的数据,用值传递,否则使用地址传递
结构体中const使用场景
作用:用const来防止误操作
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
void printStudent(const student *stu)
{
/*
./demo6.cpp: In function ‘void printStudent(const student*)’:
./demo6.cpp:14:16: error: assignment of member ‘student::score’ in read-only object
14 | stu->score = 100;
| ~~~~~~~~~~~^~~~~
*/
// stu->score = 100; // 报错,只读
cout << "name:" << stu->name << " , age:" << stu->age << " , score:" << stu->score << endl;
}
int main()
{
struct student stu = {"张三", 18, 95};
printStudent(&stu);
return 0;
}