8 结构体
时间:2023-9-18 22:32 作者:wen 分类: C++
[toc]
结构体
结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
结构体定义和使用
语法:struct 结构体名 变量名
通过结构体创建变量的方式有三种:
struct 结构体名 变量名
struct 结构体名 变量名 = {成员值1,成员值2}
- 定义结构体是顺便创建变量
示例
#include <iostream>
using namespace std;
// 结构体
struct Student
{
// 成员列表
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
};
int main()
{
//struct 结构体名 变量名
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名: " << s1.name << " 年龄: " << s1.age << " 分数: " << s1.score << endl;
//struct 结构体名 变量名 = { 成员值1,成员值2 }
struct Student s2 = { "李四",19,80 };
cout << "姓名: " << s2.name << " 年龄: " << s2.age << " 分数: " << s2.score << endl;
//定义结构体是顺便创建变量,这个比较少用
struct Teacher
{
// 成员列表
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
} s3;
s3.name = "王五";
s3.age = 20;
s3.score = 90;
cout << "姓名: " << s3.name << " 年龄: " << s3.age << " 分数: " << s3.score << endl;
system("pause");
return 0;
}
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] = {{},{},...,{}}
示例
#include <iostream>
using namespace std;
// 结构体数组
struct Student
{
// 成员列表
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
};
int main()
{
struct Student stuArray[3] =
{
{"张三",18,100},
{"李四",19,90},
{"王五",20,80}
};
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;
// 结构体数组
struct Student
{
// 成员列表
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
};
int main()
{
struct Student stuArray[3] =
{
{"张三",18,100},
{"李四",19,90},
{"王五",20,80}
};
struct Student* p = stuArray;
for (int i = 0; i < 3; i++)
{
cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
cout << "姓名: " << (*p).name << " 年龄: " << (*p).age << " 分数: " << (*p).score << endl;
p++;
}
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 = { 1000,"老王",50,{"张三",18,100} };
cout << "老师姓名: " << t.name << " 老师编号: " << t.id << " 老师年龄: " << t.age << endl;
cout << "老师辅导的学生: " << t.stu.name << " 学生的年龄: " << t.stu.age << " 学生的分数: " << t.stu.score << endl;
system("pause");
return 0;
}
结构体做函数参数
作用: 将结构体作为参数向函数中传递。
传递方式有两种:
- 值传递
- 地址传递
示例
#include <iostream>
using namespace std;
struct Student
{
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
};
void printStu(Student stu)
{
cout << "姓名: " << stu.name << " 年龄: " << stu.age << " 分数: " << stu.score << endl;
}
void printStuA(Student* stu)
{
cout << "姓名: " << stu->name << " 年龄: " << stu->age << " 分数: " << stu->score << endl;
}
int main()
{
Student s = { "张三", 18 ,100 };
printStu(s);
printStuA(&s);
system("pause");
return 0;
}
结构体中const使用场景
作用:用const来防止误操作
示例
#include <iostream>
using namespace std;
struct Student
{
// 姓名
string name;
// 年龄
int age;
// 分数
int score;
};
// 加入const,一旦有修改就会报错
void printStuA(const Student* stu)
{
cout << "姓名: " << stu->name << " 年龄: " << stu->age << " 分数: " << stu->score << endl;
}
int main()
{
Student s = { "张三", 18 ,100 };
printStuA(&s);
system("pause");
return 0;
}
结构体案例
案例1
案例描述:
学校正在做毕设项目,每名老师带领五个学生,总共有三名老师。需求如下,
设计学生和老师的结构体,其中在老师的结构体中有老师姓名和一个存放五名学生的数组作为成员,
学生的成员有姓名,考试分数。创建数组存放三名老师,通过函数给每个老师提所带的学生赋值
最终打印出老师数据,以及老师所带的学生数据。
示例:
#include <iostream>
using namespace std;
struct Student
{
// 姓名
string name;
// 分数
int score;
};
struct Teacher
{
string name;
Student stu[5];
};
// 加入const,一旦有修改就会报错
void printStuA(const Student* stu)
{
cout << "\t姓名: " << stu->name << " 分数: " << stu->score << endl;
}
void printTeacherA(const Teacher* t)
{
cout << "老师的姓名: " << t->name << endl;
cout << "老师名下的学生:" << endl;
for (int i = 0; i < 5; i++)
{
printStuA(&t->stu[i]);
}
}
int main()
{
srand((unsigned)time(0));
Teacher tArray[3];
string ext = "ABCDE";
for (int i = 0; i < 3; i++)
{
tArray[i].name = "Teacher_";
tArray[i].name += ext[i];
for (int j = 0; j < 5; j++)
{
string temp = "Stu_";
temp += ext[i];
temp += "_";
temp += ext[j];
int random = rand() % 61 + 40;
tArray[i].stu[j] = { temp,random };
}
}
for (int i = 0; i < 3; i++)
{
printTeacherA(&tArray[i]);
}
system("pause");
return 0;
}
案例2
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别。创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按年龄进行升序排序,最终打印排序后的结果。
五名英雄信息如下:
{
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 20, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"},
}
示例:
#include <iostream>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
void printHero(const Hero* h)
{
cout << "姓名: " << h->name << " 年龄: " << h->age << " 性别: " << h->sex << endl;
}
void bubbleSort(Hero heroArray[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++) {
if (heroArray[j].age > heroArray[j + 1].age)
{
Hero temp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = temp;
}
}
}
}
int main()
{
Hero hero[5] = {
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 20, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"},
};
cout << "排序前: " << endl;
for (int i = 0; i < 5; i++)
{
printHero(&hero[i]);
}
bubbleSort(hero, 5);
cout << "排序后: " << endl;
for (int i = 0; i < 5; i++)
{
printHero(&hero[i]);
}
system("pause");
return 0;
}
标签: C/C++