1. pair的定义和结构
• 基础概念 :考察对std::pair
模板类的理解,包括其头文件(<utility>
)和基本语法(pair<T1, T2>
)。
• 成员访问 :测试对first
和second
成员变量的使用能力。
• 构造方式 :如何通过构造函数或make_pair()
函数创建pair
对象(如 pair<int, string> p(1, "test")
)。
2. pair的嵌套
• 多层嵌套结构 :考察pair
与其他容器(如vector
、map
)或自身嵌套的能力(如 pair<int, pair<string, float>>
)。
• 复杂数据组织 :可能涉及在嵌套pair
中访问多层数据(如 p.second.first
)。
• 实际应用场景 :例如用pair
存储坐标点(pair<int, int>
)或键值对的组合(pair<string, map<int, float>>
)。
3. pair的自带排序规则
• 默认比较逻辑 :理解pair
的默认排序规则(先按first
升序,若相等再按second
升序)。
• 自定义排序 :在需要打破默认规则时(如降序排序),如何通过自定义比较函数或Lambda表达式实现。
• 结合STL算法 :例如在sort()
函数中使用pair
的排序特性处理容器数据。
考试题型推测
- 选择题/填空题 :考察
pair
的定义、成员变量、默认行为等基础知识点。 - 简答题 :解释
pair
的排序规则或嵌套应用场景。 - 编程题 :完成基于
pair
的算法实现或数据操作。 - 代码分析题 :阅读并改进包含
pair
的代码逻辑。
重点应用方向
• STL容器结合 :pair
与map
、vector
等容器的联合使用(如map
的键值对本质是pair
)。
• 算法优化 :利用pair
简化多属性数据的比较和排序逻辑。
• 工程实践 :在数据结构设计中灵活使用pair
嵌套(如树节点、图边权重的组合存储)。
一些测试题
一、选择题
-
std::pair
的头文件是?A.
<algorithm>
B.
<utility>
C.
<vector>
D.
<map>
答案:B -
如何访问
pair
对象的第二个成员?A.
p.first
B.
p.second
C.
p.value
D.
p.key
答案:B -
pair
的默认排序规则是?A. 先按
second
升序,再按first
升序B. 先按
first
升序,再按second
升序C. 按内存地址排序
D. 无序
答案:B
二、填空题
-
声明一个存储
int
和string
的pair
对象:
答案:pair<int, string> p;
-
通过函数创建
pair
对象的语法:auto p = ___________(3, "hello");
答案:make_pair
-
访问嵌套
pair
的示例:pair<int, pair<char, float>> p;
,要获取char
值应写为:________
答案:p.second.first
三、简答题
-
简述
pair
的默认排序规则,并举例说明。
答案:默认按
first
成员升序排序,若first
相等则按second
升序。例如:
pair<int, int>(2,3)
会排在pair<int, int>(2,5)
之前。 -
如何对
vector<pair<int, string>>
按first
降序排序?写出代码片段。
答案:cppsort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.first > b.first; });
四、编程题
-
题目: 定义一个嵌套
pair
结构pair<string, pair<int, float>>
,表示学生姓名、年龄和成绩。创建包含3个此类对象的vector
,并按年龄升序排序后输出。
参考答案:cpp#include <iostream> #include <utility> #include <vector> #include <algorithm> using namespace std; int main() { vector<pair<string, pair<int, float>>> students = { {"Alice", {20, 88.5}}, {"Bob", {18, 90.0}}, {"Charlie", {22, 85.0}} }; sort(students.begin(), students.end(), [](const auto& a, const auto& b) { return a.second.first < b.second.first; }); for (const auto& s : students) { cout << s.first << ": Age=" << s.second.first << ", Score=" << s.second.second << endl; } return 0; }
机器人的代码看不太懂
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
//pair<string,pair<int,float>>stu;
vector<pair<string,pair<int,float>> >stu;
stu.push_back({"xiaoMing",{23,89.9}});
stu.push_back({"xiaoHong",{21,88.5}});
stu.push_back({"xiaoTong",{28,98.5}});
auto compare = [](const pair<string, pair<int, float>>& a, const pair<string, pair<int, float>>& b) {
return a.second.first < b.second.first; // 比较年龄
};
// 使用sort函数进行排序
sort(stu.begin(), stu.end(), compare);
for (const auto& student : stu) {
cout << "Name: " << student.first << ", Age: " << student.second.first << ", Score: " << student.second.second << endl;
}
return 0;
}
-
题目: 使用
pair
存储坐标点(x, y)
,对vector<pair<int, int>>
按x
升序排序,若x
相同则按y
降序排序。
参考答案:cppsort(coords.begin(), coords.end(), [](const auto& a, const auto& b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; });
五、纠错题
找出以下代码的错误并修正:
cpp
pair<int, string> p = {5, "test"};
cout << p.second() << endl; // 试图输出second成员
答案:
错误:second
是成员变量而非函数,应去掉()
。
修正:cout << p.second << endl;
这个代码也不错
c
#include <iostream>
#include <utility>
#include <vector>
// 定义一个结构体表示人的信息
struct Person {
std::string name;
int age;
};
int main() {
// 创建一个存储Person对象的向量
std::vector<Person> people;
people.push_back({"Alice", 25});
people.push_back({"Bob", 30});
people.push_back({"Charlie", 20});
// 创建一个存储pair的向量,每个pair包含Person对象和评分
std::vector<std::pair<Person, int>> scores;
scores.push_back({people[0], 98});
scores.push_back({people[1], 85});
scores.push_back({people[2], 95});
// 遍历输出每个pair的内容
for (const auto& entry : scores) {
std::cout << "Name: " << entry.first.name
<< ", Age: " << entry.first.age
<< ", Score: " << entry.second << std::endl;
}
return 0;
}