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
降序排序?寫出代碼片段。
答案:sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {return a.first > b.first; });
四、編程題
- 題目: 定義一個嵌套
pair
結構pair<string, pair<int, float>>
,表示學生姓名、年齡和成績。創建包含3個此類對象的vector
,并按年齡升序排序后輸出。
參考答案:#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; }
機器人的代碼看不太懂
#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
降序排序。
參考答案:sort(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; });
五、糾錯題
找出以下代碼的錯誤并修正:
pair<int, string> p = {5, "test"};
cout << p.second() << endl; // 試圖輸出second成員
答案:
錯誤:second
是成員變量而非函數,應去掉()
。
修正:cout << p.second << endl;
這個代碼也不錯
#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;
}