结构体
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
整型、浮点型、布尔型、字符型、字符串型。系统内置的类型。
一些类型集合组成的一个类型。
#include<iostream>
using namespace std;
#include<string>
struct Student
{
//成员列表
string name;
int age;
int score;
}s3;
int main()
{
// 通过学生类型创建具体学生。三种方式:
// 1、struct Student s1
// 2、struct Student s2 = {...}
// 3、在定义结构体时顺便创建结构体变量。
struct Student s1; // 给s1属性赋值。
s1.name = "张三";
s1.age = 18;
s1.score = 99;
cout << "姓名:" << s1.name << ",年龄:" << s1.age << ",成绩:" << s1.score << "。" << endl;
struct Student s2 = { "李四", 20, 100 };
cout << "姓名:" << s2.name << ",年龄:" << s2.age << ",成绩:" << s2.score << "。" << endl;
s3.name = "王五";
s3.age = 17;
s3.score = 99;
cout << "姓名:" << s3.name << ",年龄:" << s3.age << ",成绩:" << s3.score << "。" << endl;
// 创建变量时,关键字struct可以省略。
Student s4;
s4.name = "赵六";
s4.age = 19;
s4.score = 100;
cout << "姓名:" << s4.name << ",年龄:" << s4.age << ",成绩:" << s4.score << "。" << endl;
system("pause");
return 0;
}
结构体数组
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
int main()
{
struct Student stuArray[3] = {
{"张三", 19, 90 },
{"李四", 20, 99 },
{"王五", 18, 96 }
};
stuArray[1].name = "赵六";
stuArray[0].age = 99;
stuArray[2].score = 100;
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name
<< ",年龄:" << stuArray[i].age
<< ",成绩:" << stuArray[i].score
<< "。" << endl;
}
system("pause");
return 0;
}
结构体指针
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
int main()
{
// 指针指向结构体变量。
// 通过指针访问结构体变量中的数据。 -> 访问符
struct Student s = { "张三", 19, 200 };
struct Student* p = &s;
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 stu; // 辅导的学生。
};
int main()
{
Student st = { "张三", 20, 99 };
Teacher te;
te.id = 100;
te.name = "老王";
te.age = 50;
te.stu = st;
cout << "老师 " << te.name << " 辅导的学生 " << te.stu.name << " 成绩为:" << te.stu.score << endl;
system("pause");
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递
- 值传递 :不改变实参。
- 地址传递 :形参改变,实参也跟着变。
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
void PrintStudentInfo(Student s)
{
s.name = "赵四";
cout << "子函数中结构体参数值:" << s.name << s.age << ":" << s.score << endl;
}
void PrintStudentInfo1(Student* s)
{
s->name = "尼古拉斯赵四";
s->age = 100;
cout << "子函数1中结构体参数值:" << s->name << s->age << ":" << s->score << endl;
}
int main()
{
Student s = { "张三", 19, 99 };
cout << "main中结构体实参值:" << s.name << s.age <<":"<< s.score << endl;
PrintStudentInfo(s);
PrintStudentInfo1(&s);
cout << "main中结构体实参值:" << s.name << s.age << ":" << s.score << endl;
system("pause");
return 0;
}
牢记:值传递形参不影响实参,地址传递形参会影响实参。
结构体中const使用场景
作用:用const来防止误操作。
const使用场景:
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
void printStuInfo(Student s)
{
s.age = 10000;
cout << s.name << "的年龄:" << s.age << "。成绩为:" << s.score << endl;
}
int main()
{
Student s = { "张三", 19, 99 };
// 通过函数来打印结构变量的信息。
printStuInfo(s);
system("pause");
return 0;
}
问题来了:
假设:现在有一个学生的数组,这个结构体数组很大。数组中每个结构体都要去调用这个函数。那么在实际执行时,传入的每一个参数都要在内存中开辟一个空间,用来复制存储这个结构体。而如果这个结构体如果很大,有很多信息时,这样占据的内存空间也很大,同时及进行时系统负担会很重,所以,一般的做法是,直接把指针传递过去。因为一个指针只占四个字节。
将函数中的形参改为指针,可以减少内存空间。而且不会复制新的数据。
为了防止函数误操作,写(修改)实参的值。将函数传入的指针参数用const来修饰。
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
void printStuInfo(Student s)
{
s.age = 10000;
cout << s.name << "的年龄:" << s.age << "。成绩为:" << s.score << endl;
}
// 将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来。
void printStuInfo1(const Student* s)
{
//s->age = 10000; // 加入const之后,一旦有修改的操作就会报错,可以防止我们的误操作。
cout << s->name << "的年龄:" << s->age << "。成绩为:" << s->score << endl;
}
int main()
{
Student s = { "张三", 19, 99 };
// 通过函数来打印结构变量的信息。
printStuInfo(s);
printStuInfo1(&s);
system("pause");
return 0;
}
本节:两个重点:
- 函数使用地址传递,节省内存空间。
- 函数中的参数使用const修饰,可以防止函数误操作地址传递的值。
结构体案例一:
struct Student
{
string name;
int score;
};
struct Teacher
{
string name;
Student sArray[5];
};
void allocateSpace(Teacher tArray[] , int len)
{
string tName = "教师";
string sName = "学生";
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].name = tName + nameSeed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].name = sName + nameSeed[j];
tArray[i].sArray[j].score = rand() % 61 + 40; // 随机数(0~60)+ 40的数。
}
}
}
void printTeachers(Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << tArray[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
Teacher tArray[3]; //老师数组
int len = sizeof(tArray) / sizeof(Teacher);
allocateSpace(tArray, len); //创建数据
printTeachers(tArray, len); //打印数据
system("pause");
return 0;
}
结构体案例二
//英雄结构体
struct hero
{
string name;
int age;
string sex;
};
//冒泡排序
void bubbleSort(hero arr[] , int len) // hero arr[] 也可以写成 hero* arr.
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (arr[j].age > arr[j + 1].age)
{
hero temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//打印数组
void printHeros(hero arr[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名: " << arr[i].name << " 性别: " << arr[i].sex << " 年龄: " << arr[i].age << endl;
}
}
int main() {
struct hero arr[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(arr) / sizeof(hero); //获取数组元素个数
bubbleSort(arr, len); //排序
printHeros(arr, len); //打印
system("pause");
return 0;
}
可以参照:指针笔记四:指针与数组和函数
冒泡排序传入的参数都是地址:两种形式:hero arr[] 和hero* arr都是指针的形式。所以可以直接修改实参内容。