结构体定义和使用
结构体基本概念
结构体属于用户自己定义的数据类型,允许用户存储不同的数据类型
语法:
struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:
1.struct 结构体名 变量名
2.struct结构体名 变量名={成员1值,成员2值…}
3.定义结构体时顺便创建变量(一般不建议用)
具体例子:
思路步骤:
1.创建学生数据类型
2.通过学生类型创建具体学生
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
struct Student//此处关键字struct可以省略
{
//成员列表
//姓名
string name;//用string注意添加头文件(#include <string>)
//年龄
int age;
//分数
int score;
}s3;//顺便创建结构体变量
//2.通过学生类型创建具体学生
//2.1 struct Student s1
struct Student s1;
//给s1属性赋值,通过.访问结构体变量中的属性
s1.name="zhang";
s1.age=18;
s1.score=100;
cout<<"name:"<<s1.name<<"age:"<<s1.age<<"score :"<<s1.score<<endl;
//2.2 struct Student s2={...}
struct Student s2={"li",19,80};
cout<<"name:"<<s1.name<<"age:"<<s1.age<<"score :"<<s1.score<<endl;
//2.3定义结构体时顺便创建结构体变量
s3.name="wang";
s3.age=20;
s3.score=70;
cout<<"name:"<<s1.name<<"age:"<<s1.age<<"score :"<<s1.score<<endl;
总结
1:定义结构体时的关键字是struct,不可以省
2:创建结构体变量时,关键字struct可以省略
3:结构体变量利用操作符”.”访问成员
结构体数组
作用:
将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[元素个数]={{},{}…{}}
#include <iostream>
#include <string>
using namespace std;
//结构体数组
//1、定义结构体
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//2、创建结构体数组
struct Student stuArray[3]=
{
{"zhang",18,100},
{"li",28,99},
{"wang",20,88}
};
//3、给结构体数组中的元素赋值
stuArray[2].name="zhao";
stuArray[2].age=19;
stuArray[2].score=80;
//4、遍历结构体数组
for(int i=0;i<3;i++)
{
cout<<"name:"<<stuArray[i].name
<<" age:"<<stuArray[i].age
<<" score:"<<stuArray[i].score<<endl;
}
}
结构体指针
作用:
通过访问指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
具体举例:
#include <iostream>
#include <string>
using namespace std;
//结构体指针
//定义学生的结构体
struct Student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//1.创建结构体变量
struct Student s={"zhang",18,100};
//2.通过指针指向结构体变量
Student *p =&s;
//3.通过指针访问结构体变量中的数据
cout<<"name:"<<p->name<<" age:"<<p->age<<" score:"<<p->score<<endl;
}
结构体嵌套结构体
作用:
结构体中的成员可以时另一个结构体
例:
每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
具体举例
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//定义老师结构体
struct teacher
{
int id;//教师编号
string name;//教师姓名
int age;//年龄
struct student stu;//辅导的学生,定义在老师之前
};
int main()
{
//结构体嵌套结构体
//1.创建老师
teacher t;
t.id=10000;
t.name="D wang";
t.age=50;
t.stu.name="X wang";
t.stu.age=20;
t.stu.score=60;
cout<<"teacher's name:"<<t.name<<"teacher's id:"<<t.id<<"teacher's age:"<<t.age<<endl;
//相同可以输出学生的姓名等
}
结构体做函数参数
作用 :
将结构体作为参数向函数中传递
传递方式有两种:
1、 值传递
2、 地址传递
值传递的举例
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//打印学生信息函数
//1、值传递
void printstudent1(struct student s)
{
cout<<"姓名:"<<s.name<<"年龄:"<<s.age<<"分数 :"<<s.score<<endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上所有的信息
//创建结构体变量
struct student s;
s.name="wang";
s.age=20;
s.score=80;
printstudent1(s);
}
地址传递举例
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//打印学生信息函数
//2、地址传递
void printstudent2(struct student*p)
{
cout<<"name:"<<p->name<<" age:"<<p->age<<" score:"<<p->score<<endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上所有的信息
//创建结构体变量
struct student s;
s.name="wang";
s.age=20;
s.score=80;
printstudent2(&s);
}
总结:
如果不想修改主函数中的数据用值传递,反之,用地址传递
结构体中const应用场景
作用:
用const防止错误操作
具体举例
#include <iostream>
#include <string>
using namespace std;
//const的使用场景
//学生结构体定义
struct student
{
string name;
int age;
int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printstudents(const student *s)
{
//s->age=150;//加入const后,一旦有修改的操作就会报错,可以防止误操作
cout<<"name:"<<s->name<<" age:"<<s->age<<" score :"<<s->score<<endl;
}
int main()
{
//创建结构体变量
struct student s={"zhang",15,70};;
//通过函数打印结构体变量信息
printstudents(&s);
}