目录
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体的定义和使用
第三种使用比较少,不建议使用;
创建完结构体,在使用的时候,struct关键字可以省略👇(定义时不可省略!!!)
结构体数组
结构体指针
总结:结构体指针可以通过->操作符 来访问结构体中的成员
结构体嵌套结构体
在结构体中可以定义另一个结构体作为成员,用来解决实际问题
结构体做函数参数
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
};
//打印学生信息函数
//值传递
void printStudent1(struct Student s)
{
s.age = 100;
cout << "学生的姓名:" << s.name << " 学生的年龄:" << s.age << " 学生的分数:" << s.score << endl;
}
//打印学生信息函数
//地址传递
void printStudent2(struct Student *p)
{
p->age = 100;
cout << "学生的姓名:" << p->name << " 学生的年龄:" << p->age << " 学生的分数:" << p->score << endl;
}
int main()
{
struct Student s = { "lisa", 20, 99 };
//printStudent1(s);
printStudent2(&s);
cout << "学生的姓名:" << s.name << " main中的学生的年龄:" << s.age << " 学生的分数:" << s.score << endl;
system("pause");
return 0;//结束
}
总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
结构体中const使用场景
作用:用const来防止误操作。
用指针虽然能节省代码空间,只占4个字节即可,但改变值的时候会使原本的变量跟着改变
因此const就有用武之地了👇
加入了const之后,一旦有修改的操作就会报错,可以防止我们的误操作。
结构体案例1
自己写的版本
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int score;
};
struct Teacher
{
string name;
struct Student stu_arr[5];
};
void printmessage(struct Teacher * teacher, string t1_name, string t2_name, string t3_name)
{
teacher[0].name = t1_name;
teacher[1].name = t2_name;
teacher[2].name = t3_name;
cout << "教师1的姓名:" << teacher[0].name << " 教师2的姓名:" << teacher[1].name << " 教师3的姓名:" << teacher[2].name << endl;
}
void printmessage2(struct Teacher* teacher, string s1_name, string s2_name, string s3_name, string s4_name, string s5_name,
int s1_score, int s2_score, int s3_score, int s4_score, int s5_score)
{
teacher->stu_arr[0].name = s1_name;
teacher->stu_arr[1].name = s2_name;
teacher->stu_arr[2].name = s3_name;
teacher->stu_arr[3].name = s4_name;
teacher->stu_arr[4].name = s5_name;
teacher->stu_arr[0].score = s1_score;
teacher->stu_arr[1].score = s2_score;
teacher->stu_arr[2].score = s3_score;
teacher->stu_arr[3].score = s4_score;
teacher->stu_arr[4].score = s5_score;
}
int main()
{
struct Teacher tea_arr[3];
printmessage(tea_arr, "Mr.LI", "Mr.ZHANG", "Mr.GAO");
printmessage2(&tea_arr[0], "张三", "李四", "王五", "刘六", "高七", 90, 80, 10, 100, 20);
printmessage2(&tea_arr[1], "阿三", "阿四", "阿五", "阿六", "阿七", 70, 60, 70, 30, 40);
printmessage2(&tea_arr[2], "王三", "何四", "余五", "丁六", "林七", 55, 12, 77, 53, 95);
cout << " 教师1的学生1的姓名:" << tea_arr[0].stu_arr[0].name << " 他的分数为 " << tea_arr[0].stu_arr[0].score << endl << " 教师1的学生2的姓名:" << tea_arr[0].stu_arr[1].name << " 他的分数为 " << tea_arr[0].stu_arr[1].score << endl
<< " 教师1的学生3的姓名:" << tea_arr[0].stu_arr[2].name << " 他的分数为 " << tea_arr[0].stu_arr[2].score <<endl << " 教师1的学生4的姓名:" << tea_arr[0].stu_arr[3].name << " 他的分数为 " << tea_arr[0].stu_arr[3].score << endl
<< " 教师1的学生5的姓名:" << tea_arr[0].stu_arr[4].name << " 他的分数为 " << tea_arr[0].stu_arr[4].score << endl;
cout << endl;
cout << " 教师2的学生1的姓名:" << tea_arr[1].stu_arr[0].name << " 他的分数为 " << tea_arr[1].stu_arr[0].score << endl << " 教师2的学生2的姓名:" << tea_arr[1].stu_arr[1].name << " 他的分数为 " << tea_arr[1].stu_arr[1].score << endl
<< " 教师2的学生3的姓名:" << tea_arr[1].stu_arr[2].name << " 他的分数为 " << tea_arr[1].stu_arr[2].score << endl << " 教师2的学生4的姓名:" << tea_arr[1].stu_arr[3].name << " 他的分数为 " << tea_arr[1].stu_arr[3].score << endl
<< " 教师2的学生5的姓名:" << tea_arr[1].stu_arr[4].name << " 他的分数为 " << tea_arr[1].stu_arr[4].score << endl;
cout << endl;
cout << " 教师3的学生1的姓名:" << tea_arr[2].stu_arr[0].name << " 他的分数为 " << tea_arr[2].stu_arr[0].score << endl << " 教师3的学生2的姓名:" << tea_arr[2].stu_arr[1].name << " 他的分数为 " << tea_arr[2].stu_arr[1].score << endl
<< " 教师3的学生3的姓名:" << tea_arr[2].stu_arr[2].name << " 他的分数为 " << tea_arr[2].stu_arr[2].score << endl << " 教师3的学生4的姓名:" << tea_arr[2].stu_arr[3].name << " 他的分数为 " << tea_arr[2].stu_arr[3].score << endl
<< " 教师3的学生5的姓名:" << tea_arr[2].stu_arr[4].name << " 他的分数为 " << tea_arr[2].stu_arr[4].score << endl;
system("pause");
return 0;
}
效果:
黑马的版本
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
struct Student
{
string name;
int score;
};
struct Teacher
{
string name;
struct Student stu_arr[5];
};
void allocateSpace(struct Teacher tArray[], int len)
{
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].name = "Teacher_";
tArray[i].name += nameSeed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].stu_arr[j].name = "Student_";
tArray[i].stu_arr[j].name += nameSeed[j];
tArray[i].stu_arr[i].score = 60;
}
}
}
void printInfo(struct Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师的姓名:" << tArray[i].name << endl;
for (int j = 0; j < 5; j++)
{
int random = rand() % 101;//0~100
tArray[i].stu_arr[j].score = random;
cout << "\t学生的姓名:" << tArray[i].stu_arr[j].name << " 该学生的成绩:" << tArray[i].stu_arr[j].score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
struct Teacher tea_arr[3];
int len = sizeof(tea_arr) / sizeof(tea_arr[0]);
allocateSpace(tea_arr, len);
printInfo(tea_arr, len);
system("pause");
return 0;
}