结构体的定义和使用
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
结构体定义语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:
1、struct 结构体名 变量名;
2、struct 结构体名 变量名={成员1值,成员2值,…};
3、定义结构体时顺便创建变量。
使用:
1、定义结构体时的关键字是struct,不可省略;
2、创建结构体变量时,关键字struct可以省略;
3、结构体变量利用操作符“ . ”访问成员。
#include<iostream>
using namespace std;
#include<string>
//创建学生数据类型
//自定义数据类型,一些类型集合组成的一个类型
struct Student
{
//成员列表
string name;
int age;
int score;
}s3; //s3结构体变量创建方法3
int main()
{
//1、struct Student s1;
struct Student s1; //struct关键字可以省略
s1.name = "张三";
s1.age = 20;
s1.score = 90;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//2、struct Student s2={...};
struct Student s2 = { "李四",21,95 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
//3、在定义结构体时顺便创建结构体变量
s3.name = "王五";
s3.age = 22;
s3.score = 99;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
system("pause");
return 0;
}
输出:
姓名:张三 年龄:20 分数:90
姓名:李四 年龄:21 分数:95
姓名:王五 年龄:22 分数:99
结构体数组
作用:将自定义的结构体放入到数组中方便维护。
语法:struct 结构体名 数组名{元素个数}={{ },{ },…{ }};
#include<iostream>
using namespace std;
#include<string>
//创建学生数据类型
struct Student
{
string name;
int age;
int score;
};
int main()
{
//创建结构体数组
struct Student stuArray[3] =
{
{"张三",18,90},
{"李四",19,91},
{"王五",20,92}
};
stuArray[0].name = "赵六";
stuArray[0].age = 30;
stuArray[0].score = 99;
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name
<< " 年龄:" << stuArray[i].age
<< " 分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
输出:
姓名:赵六 年龄:30 分数:99
姓名:李四 年龄:19 分数:91
姓名:王五 年龄:20 分数:92
结构体指针
作用:通过指针访问结构体的成员。
利用操作符 -> 可以通过结构体指针访问结构体属性。
#include<iostream>
using namespace std;
#include<string>
//创建学生数据类型
struct Student
{
string name;
int age;
int score;
};
int main()
{
//1、创建学生结构体变量
struct Student s = { "张三",18,100 };
//2、通过指针指向结构体变量
struct Student* p = &s; //关键字struct可以省略
//3、通过指针访问结构体变量中的数据
//通过结构体指针访问结构体中的属性,需要利用"->"
cout << "姓名:" << p->name
<< " 年龄:" << p->age
<< " 分数:" << p->score << endl;
system("pause");
return 0;
}
结构体嵌套结构体
作用:在结构体中可以定义另一个结构体作为成员,用来解决实际问题。
例:老师辅导学生,一个老师的结构体中,记录一个学生的结构体。
#include<iostream>
using namespace std;
#include<string>
//定义学生结构体
struct student
{
string name;
int age;
int score;
};
//定义老师结构体
struct teacher
{
int id;
string name;
int age;
struct student s; //老师辅导的学生
};
int main()
{
//创建老师结构体变量
teacher t;
t.id = 007;
t.name = "老王";
t.age = 36;
t.s.name = "小王";
t.s.age = 18;
t.s.score = 99;
system("pause");
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递。
传递方式有两种:值传递和地值传递。
#include<iostream>
using namespace std;
#include<string>
//定义学生结构体
struct student
{
string name;
int age;
int score;
};
//值传递
void print1(struct student s)
{
cout << "在子函数中 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}
//地址传递
void print2(struct student* p)
{
cout << "在子函数中 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main()
{
//创建结构体变量
struct student s;
s.name = "小四";
s.age = 36;
s.score = 99;
print1(s);
print2(&s);
system("pause");
return 0;
}
结构体中const使用场景
作用:用const来防止误操作。
#include<iostream>
using namespace std;
#include<string>
//定义学生结构体
struct student
{
string name;
int age;
int score;
};
//值传递的形参不改变实参
void print(const student *s)//加const防止函数体中的误操作
{
s->age = 66;//操作失败,因为加了const修饰
}
int main()
{
//创建结构体变量
struct student s = {"张三",18,95};
print(&s);
system("pause");
return 0;
}