DAY9-预习
2026-07-19 20:55:43
发布于:广东
📦 C++ 结构体与联合体 · 课前预习笔记
明天要学的东西叫 “结构体”和“联合体” ——说白了就是让程序把一堆相关的数据打包成一个新类型,比如一个学生的姓名、年龄、成绩,可以打包成“学生类型”,用起来特别方便!
一、先搞懂:为什么需要结构体?
1. 没有结构体的时候有多麻烦?
假设你要存一个学生的信息:姓名、年龄、身高、3门课成绩。
string name = "张三";
int age = 15;
double height = 170.5;
int score1 = 90, score2 = 85, score3 = 88;
如果存3个学生呢?变量多到爆炸!😭
string name1, name2, name3;
int age1, age2, age3;
double height1, height2, height3;
// ... 手要断了!
2. 有结构体之后多爽!
struct Student {
string name;
int age;
double height;
int scores[3];
};
Student s1 = {"张三", 15, 170.5, {90, 85, 88}};
Student s2 = {"李四", 16, 165.0, {78, 92, 80}};
// 一个变量搞定一个学生的所有信息!爽!
💡 结构体 = 把多个不同类型的变量打包成一个新类型,就像把散装零件组装成一个完整的工具箱。
二、结构体的定义与使用
1. 怎么定义结构体?
语法格式:
struct 结构体类型名 {
数据类型 成员1;
数据类型 成员2;
// ... 更多成员
};
例子:定义一个“学生”类型
struct Student {
string name; // 姓名
int age; // 年龄
double height; // 身高
int score; // 分数
};
⚠️ 注意:结构体定义最后的
;不能少!这是初学者最容易忘的。
2. 怎么用结构体?
① 定义结构体变量
Student s1; // 定义一个学生变量,叫 s1
Student s2 = {"李四", 16, 175.5, 95}; // 定义并初始化
② 访问结构体成员(用 . 点号)
s1.name = "张三"; // 给 s1 的 name 赋值
s1.age = 15;
s1.height = 170.5;
s1.score = 88;
cout << s1.name << endl; // 输出:张三
cout << s1.age << endl; // 输出:15
💡 记住:用
.来访问结构体里的成员,就像“打开盒子拿东西”。
3. 完整例子
#include <iostream>
#include <string>
using namespace std;
// 定义结构体
struct Student {
string name;
int age;
double height;
int score;
};
int main() {
// 定义结构体变量并初始化
Student s1 = {"张三", 15, 170.5, 88};
Student s2;
// 给 s2 赋值
s2.name = "李四";
s2.age = 16;
s2.height = 175.0;
s2.score = 95;
// 输出信息
cout << "姓名:" << s1.name << endl;
cout << "年龄:" << s1.age << endl;
cout << "身高:" << s1.height << endl;
cout << "分数:" << s1.score << endl;
return 0;
}
三、结构体数组
1. 什么是结构体数组?
结构体数组 = 数组的每个元素都是一个结构体,比如存50个学生,每个学生都有姓名、年龄、成绩。
2. 怎么定义和使用?
Student stu[1005]; // 最多存1005个学生(下标从1开始用)
// 输入 n 个学生信息
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> stu[i].name >> stu[i].age >> stu[i].height >> stu[i].score;
}
// 输出所有学生信息
for (int i = 1; i <= n; i++) {
cout << stu[i].name << " " << stu[i].age << " "
<< stu[i].height << " " << stu[i].score << endl;
}
3. 完整例子
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
int main() {
int n;
cout << "请输入学生人数:";
cin >> n;
Student stu[1005]; // 定义结构体数组
// 输入
for (int i = 1; i <= n; i++) {
cout << "请输入第" << i << "个学生的姓名、年龄、成绩:";
cin >> stu[i].name >> stu[i].age >> stu[i].score;
}
// 输出
cout << "\n所有学生信息:" << endl;
for (int i = 1; i <= n; i++) {
cout << "姓名:" << stu[i].name
<< ",年龄:" << stu[i].age
<< ",成绩:" << stu[i].score << endl;
}
return 0;
}
四、结构体排序(⭐⭐⭐⭐⭐ 重点!)
1. 为什么要对结构体排序?
很多时候我们需要按某个规则对结构体数组排序,比如:
- 按成绩从高到低排名
- 按年龄从小到大排队
- 按姓名拼音排序
2. 用 sort + 自定义比较函数
结构体排序最常用的方法:自己写一个比较函数,告诉 sort 怎么比大小。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
// 自定义比较函数:按成绩从高到低排序
bool cmp(Student a, Student b) {
return a.score > b.score; // 分数高的排前面
// 如果要从小到大,改成 a.score < b.score
}
int main() {
int n;
cin >> n;
Student stu[1005];
for (int i = 1; i <= n; i++) {
cin >> stu[i].name >> stu[i].age >> stu[i].score;
}
// 排序:从 stu[1] 到 stu[n]
sort(stu + 1, stu + n + 1, cmp);
// 输出排序后的结果
for (int i = 1; i <= n; i++) {
cout << stu[i].name << " " << stu[i].age << " " << stu[i].score << endl;
}
return 0;
}
3. 多种排序规则
// 规则1:按成绩从高到低
bool cmp(Student a, Student b) {
return a.score > b.score;
}
// 规则2:按年龄从小到大
bool cmp(Student a, Student b) {
return a.age < b.age;
}
// 规则3:先按成绩从高到低,成绩相同按年龄从小到大
bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score > b.score; // 成绩不同,按成绩降序
}
return a.age < b.age; // 成绩相同,按年龄升序
}
// 规则4:按姓名排序(字典序)
bool cmp(Student a, Student b) {
return a.name < b.name; // string 可以直接比较!
}
五、结构体应用综合练习
题目:成绩排名系统
输入 n 个学生的姓名、语文、数学、英语成绩,要求:
- 计算每个学生的总分
- 按总分从高到低排名
- 输出排名结果
完整代码
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int chinese;
int math;
int english;
int total; // 总分
};
// 按总分从高到低排序
bool cmp(Student a, Student b) {
return a.total > b.total;
}
int main() {
int n;
cin >> n;
Student stu[1005];
// 输入并计算总分
for (int i = 1; i <= n; i++) {
cin >> stu[i].name >> stu[i].chinese >> stu[i].math >> stu[i].english;
stu[i].total = stu[i].chinese + stu[i].math + stu[i].english;
}
// 排序
sort(stu + 1, stu + n + 1, cmp);
// 输出排名
cout << "===== 成绩排名 =====" << endl;
for (int i = 1; i <= n; i++) {
cout << "第" << i << "名:" << stu[i].name
<< " 总分:" << stu[i].total
<< " (语" << stu[i].chinese
<< " 数" << stu[i].math
<< " 英" << stu[i].english << ")" << endl;
}
return 0;
}
输入:
3
张三 90 85 88
李四 78 92 80
王五 95 80 90
输出:
===== 成绩排名 =====
第1名:王五 总分:265 (语95 数80 英90)
第2名:张三 总分:263 (语90 数85 英88)
第3名:李四 总分:250 (语78 数92 英80)
六、联合体(了解)
1. 什么是联合体?
联合体(union) 和结构体很像,但所有成员共用同一块内存,同一时间只能存一个成员的值。
用大白话说:联合体就是一个“变形金刚”,同一时间只能变一种形态。
2. 怎么定义和使用?
union Data {
int i; // 4字节
double d; // 8字节
char c; // 1字节
};
int main() {
Data data;
data.i = 10; // 存整数
cout << data.i << endl; // 输出:10
data.d = 3.14; // 存小数(覆盖了之前的 int)
cout << data.d << endl; // 输出:3.14
// 注意:data.i 现在已经被覆盖了,值是乱的!
return 0;
}
3. 结构体 vs 联合体
| 对比项 | 结构体 struct | 联合体 union |
|---|---|---|
| 内存分配 | 每个成员单独占内存(加起来) | 所有成员共享一块内存(取最大的) |
| 同时使用 | 所有成员可以同时使用 | 同一时间只能用一个成员 |
| 大小 | 所有成员大小之和(+对齐) | 最大成员的大小 |
| 适用场景 | 存多个相关数据 | 存一种类型的数据(节省内存) |
struct S { int i; double d; char c; }; // 大小 ≈ 16字节
union U { int i; double d; char c; }; // 大小 = 8字节(最大的成员)
4. 什么时候用联合体?
| 场景 | 说明 |
|---|---|
| 节省内存 | 嵌入式系统、硬件编程 |
| 变体类型 | 一个变量可能存不同类型的数据 |
| 数据解析 | 把同一块内存用不同的方式解读 |
💡 了解即可,竞赛和日常编程中,联合体用得不多。重点掌握结构体!
📝 快速记忆卡
| 概念 | 一句话总结 |
|---|---|
| 结构体 | 把多个不同类型的变量打包成一个新类型 |
| 定义结构体 | struct 类型名 { 成员列表 }; |
| 访问成员 | 用 . 点号,如 s.name = "张三"; |
| 结构体数组 | Student stu[1005]; 存多个学生 |
| 结构体排序 | 写 cmp 函数,用 sort() 排 |
| 自定义比较 | bool cmp(Student a, Student b) { return a.score > b.score; } |
| 联合体 | 所有成员共用一块内存,同一时间只能用一个 |
🔥 预习小任务
- 如果让你定义一个“图书”结构体,包含:书名、作者、价格、出版年份,你会怎么写?
- 结构体排序中,
cmp函数为什么要写成a.score > b.score而不是a.score < b.score? - 结构体和联合体最大的区别是什么?
明天上课带着这些问题来,效果翻倍!🚀
这里空空如也













有帮助,赞一个