目录
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
一、结构体的定义和使用
语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的三种方式:
- struct 结构体名 变量名
- struct 结构体名 变量名 = {成员变量1,成员变量2…}
- 定义结构体时顺便创建变量
#include <iostream>
using namespace std;
struct Student // 自定义数据类型——内置数据类型的集合
{
string name;
int age;
int score;
}s3;
//建议用第一和第二种方法
int main()
{
//struct使用的关键字可以不使用
//第一种方法
struct Student s1;
s1.name = "zhuzhu";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数 :" << s1.score << endl;
//第二种方法
struct Student s2 = {"33",18,99};
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数 :" << s2.score << endl;
//第三种方法(前面定义的结构体后面加入了s3)
s3.name = "zhuzhu33";
s3.age = 18;
s3.score = 101;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数 :" << s3.score << endl;
return 0;
}
输出:
姓名:zhuzhu 年龄:18 分数 :100
姓名:33 年龄:18 分数 :99
姓名:zhuzhu33 年龄:18 分数 :101
二、结构体数组
将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名 {元素个数} = {{},{},{}…{}}
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
int score;
};
int main()
{
struct Student stuarray[3] =
{
{"zhu", 18, 99},
{"33", 20, 100},
{"zhu33", 19, 101}};
stuarray[2].age = 18; // 更改元素的值
return 0;
}
三、结构体指针
通过指针访问结构体中的成员
利用操作符 ->可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
int score;
};
int main()
{
struct Student s = {"zhu", 18, 100};
//通过指针指向结构体变量
Student *p = &s;
//通过指针来访问结构变量体中的数据
cout << "name:" << p->name << " age:" << p->age << " score:" << p->score << endl;
return 0;
}
输出:
name:zhu age:18 score:100
四、结构体嵌套结构体
结构体中的成员可以是另一个结构体
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
int age;
struct student stu; //学生的结构体(这里面的student就是上面定义的student的结构体)
};
int main()
{
struct teacher t;
t.id = 18;
t.name = "zhuzhu";
t.age = 18;
t.stu.age = 12; // 这下面的三个就是学生定义的结构体的三个成员
t.stu.name = "zhu";
t.stu.score = 100;
return 0;
}
五、结构体做函数参数
将结构体作为参数向函数中传递
传递的方式有两种:
- 值传递
- 地址传递
如果想要修改结构体中的数据,就用地址传递!
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//打印学生信息的函数
//值传递
void printstudent1(struct student s)
{
s.age = 100; // 这里在子函数中会修改,但是main函数中不会修改
cout << "在子函数1中_姓名:" << s.name << " 年龄:" << s.age << " 分数 :" << s.score << endl;
}
//地址传递
void printstudent2(struct student *p)
{
p->age = 200; // 这里在子函数中会修改,main函数中也会修改
cout << "在子函数2中_姓名:" << p->name << " 年龄:" << p->age << " 分数 :" << p->score << endl;
}
int main()
{
student s = {"zhuzhu" , 18 , 100};
cout << "在main1函数中_姓名:" << s.name << " 年龄:" << s.age << " 分数 :" << s.score << endl;
printstudent1(s);
cout << "在main2函数中_姓名:" << s.name << " 年龄:" << s.age << " 分数 :" << s.score << endl;
printstudent2(&s);
cout << "在main3函数中_姓名:" << s.name << " 年龄:" << s.age << " 分数 :" << s.score << endl;
return 0;
}
输出:
在main1函数中_姓名:zhuzhu 年龄:18 分数 :100
在子函数1中_姓名:zhuzhu 年龄:100 分数 :100
在main2函数中_姓名:zhuzhu 年龄:18 分数 :100
在子函数2中_姓名:zhuzhu 年龄:200 分数 :100
在main3函数中_姓名:zhuzhu 年龄:200 分数 :100
六、结构体中的const使用场景
用const来防止误操作
用指针是为了减少内存空间,值传递占用内存大(结构体多大,传递就多大)
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//将函数中的形参改为指针可以减少内存空间,而且不会复制一个新的副本出来
void printstudent( const student *s)
{
// s->age = 100; //表达式必须是可修改的左值
//加入const之后一旦误操作修改,就会出现“表达式必须是可修改的左值”的错误报告
cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数 :" << s->score << endl;
}
int main()
{
struct student s = {"zhu", 18, 100};
printstudent(&s);
return 0;
}