目录
8 结构体
8.1 结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
8.2 结构体定义和使用
语法:
struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:
-
struct结构体名 变量名
-
struct结构体名 变量名 = {成员1值,成员2值...}
-
定义结构体时顺便创建变量
示例:
//结构体定义
//语法 struct 类型名称 {成员列表}
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;
/2、通过学生类型创建具体学生
int main()
{
//2.1 struct Student s1
//struct关键字可以省略,
struct Student 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 = 20;
s3.score = 98;
cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
system("pause");
return 0;
}
总结1:定义结构体时关键字是struct,不可省略
总结2:创建结构体变量时,关键字struct可以省略
总结3:结构体变量利用操作符“.”访问成员
8.3 结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[元素个数] = { {},{},{}....}
示例:
//结构体定义
struct Student
{
//成员列表
string name;//姓名
int age;//年龄
int score;//分数
};
int main()
{
//2、创建结构体数组
struct Student stuArray[3] =
{
{"zhangsan",18,100},
{"lisi",20,78},
{"wangwu",19,84}
};
//3、给结构体数组中的元素赋值
stuArray[0].name = "zhaoliu";
stuArray[1].age = 18;
stuArray[2].score = 54;
//4、遍历结构体数组中的元素
for (int i = 0; i < 3; i++)
{
cout << "name: " << stuArray[i].name << "age: " << stuArray[i].age << "score: " << stuArray[i].score << endl;
}
system ("pause");
return 0;
}
8.4 结构体指针
作用:通过指针访问结构体中的成员
-
利用操作符 -> 可以通过结构体指针访问结构体属性
示例:
//结构体指针
//结构体定义
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//1、创建结构体变量
struct Student s1 = {"张三",18,78};
//2、通过指针指向结构体变量
struct Student * p = &s1;
//3、通过指针访问结构体变量中的数据
cout << " 姓名:" << p ->name << " 年龄:" << p ->age << " 成绩:" << p -> score << endl;
system("pause");
return 0;
}
总结:结构体指针可以通过 ->操作符来访问结构体中的成员
8.5 结构体嵌套结构体
作用:结构体的成员可以是另一个结构体
例如:每个老师辅导一个学生,一个老师的结构体中,记录一个学生的结构体
示例:
//定义学生结构体
struct studen
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//定义教师结构体
struct teacher
{
//成员列表
//职工编号
int id;
//教师姓名
string name;
//教师年龄
int age;
struct studen stu;//子结构体 辅导的学生
};
int main()
{
//结构体嵌套结构体
//创建老师
teacher t;
t.id = 1;
t.name = "李四";
t.age = 24;
t.stu.age = 10;
t.stu.name = "张三";
t.stu.score = 98;
cout << "老师姓名:" << t.name << " 职工编号:" << t.id << " 老师的年龄:" << t.age
<< " 老师辅导的学生姓名:" << t.stu.name << " 学生的年龄:" << t.stu.age << " 学生的成绩: " << t.stu.score
<< endl;
system ("pause");
return 0;
}
总结:在结构体中可以定义另一个结构体作为成员,用来解决实际问题
8.6 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
-
值传递
-
地址传递
使用值传递时改变在函数内输入改变年龄主函数中的年龄也不会改变
示例:
//定义学生结构体
struct student
{
string name;//姓名
int age;//年龄
int score;//分数
};
//打印函数
//值传递
void PrintStu1(struct student s)
{
s.age = 100;
cout << "子函数中 姓名:" << s.name << " 年龄:" << s.age << " 成绩:" << s.score << endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有属性
//创建结构体变量
struct student s;
s.name = "张三";
s.age = 20;
s.score = 98;
PrintStu1(s);
PrintStu2(&s);
cout << "在main函数中的打印 姓名:" << s.name <<" 年龄:" << s.age << " 成绩:" << s.score << endl;
system("pause");
return 0;
}
使用地址传递时改变地址传递函数中的年龄,主函数的年龄也会改变。
示例:
//定义学生结构体
struct student
{
string name;//姓名
int age;//年龄
int score;//分数
};
//打印函数
//地址传递
void PrintStu2(struct student * p)
{
p -> age = 35;
cout << "子函数中地址传递 姓名:" << p -> name << " 年龄:" << p -> age << " 成绩:" << p -> score << endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有属性
//创建结构体变量
struct student s;
s.name = "张三";
s.age = 20;
s.score = 98;
PrintStu1(s);
PrintStu2(&s);
cout << "在main函数中的打印 姓名:" << s.name <<" 年龄:" << s.age << " 成绩:" << s.score << endl;
system("pause");
return 0;
}
总结:如果不想修改主函数中的数据,用值传递,反之地址传递
8.7 结构体中 const使用场景
作用:用const来防止误操作
示例:
//const使用场景
//学生结构体定义
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
//打印函数
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void PrintStudent(const struct student * p)
{
//p -> age = 100;加入const之后,已大有修改的操作就会报错,可以防止误操作
cout << "姓名:" << p -> name << " 年龄:" << p -> age << " 成绩:" << p -> score << endl;
}
int main()
{
//创建结构体变量
student s;
s.name = "李四";
s.age = 20;
s.score = 87;
//通过函数打印结构体变量信息
PrintStudent(&s);
cout << "在main函数中 姓名:" << s.name << " 年龄:" << s.age << " 成绩:" << s.score << endl;
system ("pause");
return 0;
}