一、结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
二、结构体的定义和使用
语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:
- struct 结构体名 变量名
- struct 结构体名 变量名={成员1值,成员2值...}
- 定义结构体时顺便创建变量
注意:
- 定义结构体时的关键字struct,不可省略
- 创建结构体变量时,关键字struct可以省略
- 结构体变量利用操作符“.”访问成员
#include <iostream>
#include <string>
using namespace std;
//1.创建一个学生的数据类型:学生包括 (姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 struct 类型名 { 成员列表 };
struct student {
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;//在定义结构体时顺便创建结构体变量
//2.通过学生类型创建具体学生
int main() {
//2.1 struct student s1; //声明一个学生变量s1
//struct 关键字可以省略
student s1; //声明一个学生变量s1
//给s1属性赋值,通过. 访问结构体变量中的属性
s1.name="张三";
s1.age=20;
s1.score=90;
cout<<"姓名:"<<s1.name<<" 年龄:"<< s1.age<<" 分数:"<<s1.score<<endl;
//2.2 struct student s2={...}
struct student s2 = { "李四",21,85 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
//2.3 在定义结构体时顺便创建结构体变量
s3.name = "王五";
s3.age = 25;
s3.score = 85;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
system("pause");
return 0;
}
三、结构体数组
作用:将自定义结构体放到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={{ },{ },... { } }
#include <iostream>
#include <string>
using namespace std;
//结构体数组
//1.定义结构体
struct Student {
//姓名
string name;
//年龄
int age;
//成绩
int score;
};
int main() {
//2.创建结构体数组
struct Student students[8] =
{
{"张三",18,90},
{"王五",17,95},
{"赵六",16,80},
{"周六",15,70},
};
//3.给结构体数组中的元素赋值
students[2].name = "雨轩";
students[2].age = 19;
students[2].score = 85;
//4.遍历结构体数组
for (int i = 0; i < 8; i++) {
cout << "姓名:" << students[i].name << " ";
cout << "年龄:" << students[i].age << " ";
cout << "成绩:" << students[i].score << " ";
cout << endl;
}
system("pause");
return 0;
}
四、结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符->可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
//结构体指针
struct student {
string name;
int age;
float score;
};
int main() {
//创建学生的结构体变量
struct student s1 = { "张三",20,100 };
//2.通过指针指向结构体变量
struct student * p = &s1;
//3.通过指针访问结构体变量中的数据
cout<<"姓名:"<<p->name<<" 年龄:"<<p->age<<" 分数:"<<p->score<<endl;
system("pause");
return 0;
}
五、结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#include <iostream>
using namespace std;
//定义老师结构体
struct Student {
string name;//学生姓名
int age;//学生年龄
int score;//学生编号
};
struct Teacher {
int id;//教师编号
string name;//教师姓名
int age;//教师年龄
struct Student stu;//辅导的学生
};
int main() {
//结构体嵌套结构体
//创建老师
Teacher t;
t.id = 1001;
t.name = "张老师";
t.age = 30;
t.stu.name = "小王";
t.stu.age = 18;
t.stu.score = 100;
cout << "教师编号:" << t.id << endl;
cout << "教师姓名:" << t.name << endl;
cout << "教师年龄:" << t.age << endl;
cout << "辅导的学生姓名:" << t.stu.name << endl;
cout << "辅导的学生年龄:" << t.stu.age << endl;
cout << "辅导的学生成绩:" << t.stu.score << endl;
system("pause");
return 0;
}
六、结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种
- 值传递
- 地址传递
#include <iostream>
using namespace std;
//定义学生结构体
struct student {
string name;//姓名
int age;//年龄
int score;//成绩
};
//打印学生信息的函数
//值传递
void printStudent(student s) {
cout << "子函数中 姓名:" << s.name << "年龄:" << s.age << "成绩:" << s.score << endl;
}
//地址传递
void printStudent2(student* s) {
cout << "子函数中 姓名:" << s->name << "年龄:" << s->age << "成绩:" << s->score << endl;
}
int main() {
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
struct student s;
s.name = "张三";
s.age = 20;
s.score = 90;
printStudent(s);
printStudent2(&s);
//cout << "main函数中打印姓名:" << s.name << "年龄:"<<s.age<<"成绩:"<<s.score<<endl;
system("pause");
return 0;
}
值传递 和地址传递的区别
值传递代码:(值传递,形参的改变不会修改实参的值)
#include <iostream>
using namespace std;
//定义学生结构体
struct student {
string name;//姓名
int age;//年龄
int score;//成绩
};
//打印学生信息的函数
//值传递
void printStudent(student s) {
s.score = 100; //修改传入的结构体的成绩
cout << "子函数中 姓名:" << s.name << "年龄:" << s.age << "成绩:" << s.score << endl;
}
//地址传递
void printStudent2(student* s) {
cout << "子函数中 姓名:" << s->name << "年龄:" << s->age << "成绩:" << s->score << endl;
}
int main() {
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
struct student s;
s.name = "张三";
s.age = 20;
s.score = 90;
printStudent(s);
//printStudent2(&s);
cout << "main函数中打印姓名:" << s.name << "年龄:"<<s.age<<"成绩:"<<s.score<<endl;
system("pause");
return 0;
}
地址传递代码(地址传递中形参的改变会改变实参的值)
#include <iostream>
using namespace std;
//定义学生结构体
struct student {
string name;//姓名
int age;//年龄
int score;//成绩
};
//打印学生信息的函数
//值传递
void printStudent(student s) {
cout << "子函数中 姓名:" << s.name << "年龄:" << s.age << "成绩:" << s.score << endl;
}
//地址传递
void printStudent2(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 = 90;
//printStudent(s);
printStudent2(&s);
cout << "main函数中打印姓名:" << s.name << "年龄:"<<s.age<<"成绩:"<<s.score<<endl;
system("pause");
return 0;
}
七、结构体中 const的使用场景
作用:用const来防止误操作
#include <iostream>
using namespace std;
//const 的使用场景
struct student {
string name;//姓名
int age;//年龄
int score;//成绩
};
//值传递复制值会占用大量的内存资源
//将函数的形参改为指针,可以减少内存空间,而且不会复制出新的副本
void printStudent(const student *s) {
//s->age = 25; //修改结构体变量的age属性
//加入const 一旦有修改的操作就会报错,可以防止我们的误操作(只能读不能修改)
cout << "姓名:" << s->name << endl;
cout << "年龄:" << s->age << endl;
cout << "成绩:" << s->score << endl;
}
int main() {
//创造结构体变量
struct student s={"张三",20,80};
//通过函数打印结构体变量的信息
printStudent(&s);
system("pause");
return 0;
}
八、结构体案例
#include <iostream>
#include <string>
using namespace std;
//结构体案例
//学生结构体定义
struct student {
string name;//姓名
int score;//成绩
};
//老师结构体定义
struct teacher {
string tname;//姓名
student stu[5];//学生数组
};
//给老师和学生赋值的函数
void allocatSpace(teacher teachers[], int len)
{
string nameSeed = "ABCDE";
//给老师开始赋值
for (int i = 0; i < len; i++) {
teachers[i].tname = "Teacher_";
teachers[i].tname += nameSeed[i];
//给老师带的学生信息赋值
for (int j = 0; j < 5; j++) {
teachers[i].stu[j].name = "Student_";
teachers[i].stu[j].name += nameSeed[j];
int random = rand() % 61 + 40;//40~90
teachers[i].stu[j].score = random;
}
}
}
//打印所有老师及所带学生信息的函数
void printTeacher(teacher teachers[], int len)
{
for (int i = 0; i < len; i++) {
cout << "Teacher Name:" << teachers[i].tname << endl;
cout << "Students:" << endl;
for (int j = 0; j < 5; j++) {
cout << "Student Name:" << teachers[i].stu[j].name << " Score:" << teachers[i].stu[j].score << endl;
}
}
}
int main() {
//随机数种子
srand((unsigned int)time(NULL));
//1.创建3名老师的数组
teacher teachers[3];
//2.通过函数给三名老师的信息赋值,并给老师带的学生信息赋值
int len =sizeof(teachers)/sizeof(teachers[0]);
allocatSpace(teachers,len);
//3.打印所有老师及所带学生信息
printTeacher(teachers, len);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//英雄结构体定义
struct Hero {
string name;
int age;
string sex;
};
//数组冒泡排序
void bubbleSort(Hero arr[], int len ){
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
Hero temp;
if (arr[j].age>arr[j+ 1].age){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后的数组:" << endl;
for (int i = 0; i < len; i++) {
cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 性别:" << arr[i].sex << endl;
}
}
int main() {
Hero heros[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
int len = sizeof(heros) / sizeof(heros[0]);
bubbleSort(heros, len);
system("pause");
return 0;
}