结构体
//一、结构体的概念、定义和使用
// 概念:结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
#include<iostream>
using namespace std;
#include<string>
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 strcut 类型名称{成员列表}; 注意后面有;
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;
//2.通过学生类型创建具体学生
int main()
{
//2.1 struct Student s1
//strcut关键字可以省略
//struct Student s1;
Student s1;
//给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,则上面数据{}后要加;
s3.name = "王五";
s3.age = 20;
s3.score = 60;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
//一般用第1或2种
return 0;
}
//二、结构体数组
//作用:将自定义的结构体放入到数组中方便维护
//语法:struct 结构体名 数组名{元素个数}={ {},{},...{} }
#include<iostream>
using namespace std;
//结构体数组
//1.定义结构体
struct Student
{
string name;
int age;
int score;
};
int main()
{
//2.创建结构体数组
struct Student stuArray[3] =
{
{"张三",18,100},
{"李四",28,99},
{"王五",38,66}
};
//3.给结构体数组中的元素赋值
//一开始不知道人家名字,后面改
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 80;
//4.便利结构体数组
for (int i = 0; i < 3; i++)
{
cout << " 姓名: " << stuArray[i].name
<< " 年龄: " << stuArray[i].age
<< " 分数: " << stuArray[i].score << endl;
}
return 0;
}
//三、结构体指针
//作用:通过指针访问结构体中的成员 利用操作符->
#include<iostream>
using namespace std;
struct student
{
string name;
int age;
int scroe;
};
int main()
{
//1.创建学生结构体变量
struct student s = { "张三",18,100 };//struct可省略
//2.通过指针指向结构体变量
struct student* p = &s;//struct可省略
//3.通过指针访问结构体变量中的数据
//通过结构体指针 访问结构体中的属性,需要利用'->'
cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->scroe << endl;
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 = 18888;
t.name = "老王";
t.age = 50;
t.s.name = "小王";
t.s.age = 20;
t.s.score = 60;
cout << "老师姓名: " << t.name << " 老师编号: " << t.id << " 老师年龄: " << t.age
<< " 老师辅导学生姓名: " << t.s.name << " 学生年龄: " << t.s.age << " 学生分数: " << t.s.score << endl;
return 0;
}
//五、结构体做函数参数
//作用:将结构体作为参数向函数中传递
//传递方式有两种:值传递和地址传递
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
//打印学生信息函数
//1.值传递
void printStudent1(struct student s)
{
s.age = 100;
cout << " 子函数1中 姓名:" << s.name << " 年龄:" << s.age << " 分数: " << s.score << endl;
}
//2.地址传递
void printStudent2(struct student * p)
{
p->age = 200;
cout << "子函数2中 姓名:" << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
}
int main()
{
//结构题做函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
//创建一个结构体变量
struct student s;
s.name = "张三";
s.age = 18;
s.score = 85;
printStudent1(s);
printStudent2(&s);
cout << "main函数中打印 姓名:" << s.name << " 年龄: " << s.age << " 分数:" << s.score<<endl;
return 0;
}
//六、结构体中const使用场景
//作用:用const来防止误操作
#include<iostream>
using namespace std;
#include<string>
//const的使用场景
struct student
{
string name;
int age;
int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const student *s)//指针目的是节省空间,const是防止修改了形参把实参变了,即防止误操作
{
s->age = 150;//(误操作了)//但是利用了const age还是为15岁 只能读不能写
cout << "姓名: " << s->name << " 年龄: " << s->age << " 得分: " << s->score << endl;
}
int main()
{
//创建结构体变量
struct student s = { "张三",15,70 };
//通过函数打印结构变量信息
printStudent(&s);
cout << s.age;
return 0;
}