在 C++ STL(标准模板库)中,pair
是一个 简单的键值对数据结构,用于存储 两个相关联的值,将两个值组合成一个单元,可以是相同或不同类型。它常用于 返回多个值、存储映射关系、排序 等场景。
1. pair
的基本特点
-
存储两个值,可以是不同类型。
-
支持比较运算(按 第一个元素 比较,若相同则比较第二个元素)。
-
适用于存储键值对或返回多个值。
2. pair
的基本用法
2.1 pair
的定义与初始化
// 默认构造函数
pair<int, string> p1;
// 带参数的构造函数
pair<int, string> p2(10, "Hello");
// 使用make_pair函数
auto p3 = make_pair(20, "World");
//auto 自动识别类型pair<int, string>
#include <iostream>
#include <utility> // 包含 pair
using namespace std;
int main() {
pair<int, string> p1(1, "Apple");
pair<int, string> p2 = {2, "Banana"};
cout << "p1: (" << p1.first << ", " << p1.second << ")\n";
scout << "p2: (" << p2.first << ", " << p2.second << ")\n";
return 0;
}
输出:
p1: (1, Apple)
p2: (2, Banana)
pair的内部实现
pair
的实现非常简单,它是一个结构体模板,包含两个公有成员first
和second
。以下是pair
的简化实现:
template <class T1, class T2>
struct pair {
T1 first;
T2 second;
// 默认构造函数
pair() : first(T1()), second(T2()) {}
// 带参数的构造函数
pair(const T1& x, const T2& y) : first(x), second(y) {}
// 拷贝构造函数
template <class U1, class U2>
pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
};
2.2 访问 pair
的元素
pair
的成员first
和second
可以直接访问
cout << p1.first << " " << p1.second << endl;
2.3 使用 make_pair()
进行初始化
pair<int, double> p = make_pair(10, 3.14);
3. pair
在 STL 容器中的应用
3.1 pair
作为 map
的元素
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> mp;
mp.insert(pair<int, string>(1, "One"));
mp.insert(make_pair(2, "Two"));
for (auto &p : mp) {
cout << p.first << ": " << p.second << endl;
}
return 0;
}
3.2 pair
作为 vector
的元素
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<pair<int, string>> v;
v.push_back({1, "Alice"});
v.push_back({2, "Bob"});
for (auto &p : v) {
cout << p.first << " " << p.second << endl;
}
return 0;
}
3.3 返回多个值
当函数需要返回多个值时,可以使用pair
。例如,一个函数可以返回一个pair
,其中包含计算结果和状态信息。
#include <iostream>
#include <utility>
using namespace std;
pair<int, bool> divide(int a, int b) {
if (b == 0) {
return make_pair(0, false); // 返回错误状态
}
return make_pair(a / b, true); // 返回结果和成功状态
}
int main() {
auto result = divide(10, 2);
if (result.second) {
cout << "Result: " << result.first << endl;
} else {
cout << "Error: Division by zero" << endl;
}
return 0;
}
输出:
Result: 5
3.4 作为容器元素
pair
可以作为容器的元素,例如vector<pair<int, int>>
,用于存储一组相关联的值。
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
vector<pair<int, int>> vec;
vec.push_back(make_pair(1, 10));
vec.push_back(make_pair(2, 20));
for (const auto& p : vec) {
cout << "First: " << p.first << ", Second: " << p.second << endl;
}
return 0;
}
//First: 1, Second: 10
//First: 2, Second: 20
4. pair
的比较规则
pair
按 字典序 进行比较:pair
支持比较操作(==
, !=
, <
, <=
, >
, >=
)。
-
先比较
first
,小者优先。 -
若
first
相等,则比较second
。
示例:
pair<int, int> a = {1, 5};
pair<int, int> b = {1, 3};
if (a > b) {
cout << "a 更大" << endl;
} else {
cout << "b 更大" << endl;
}
输出:
a 更大
5. pair
与 tuple
的区别
特性 | pair | tuple |
---|---|---|
元素个数 | 2 | 任意个 |
访问方式 | .first .second | get<n>(tuple) |
适用场景 | 键值对、映射关系 | 多个不同类型的数据 |
6. 总结
pair
是 C++ STL 中 存储两个相关值的简单工具,可广泛用于 映射、排序、返回多个值 等场景。掌握 pair
及其 STL 结合使用方式,可以提高代码的 简洁性 和 可读性!
pair
最常见的用途是存储键值对。例如,在map
和unordered_map
中,每个元素都是一个pair
,其中first
是键,second
是值。#include <iostream> #include <map> using namespace std; int main() { map<int, string> myMap; myMap.insert(make_pair(1, "Apple")); myMap.insert(make_pair(2, "Banana")); for (const auto& p : myMap) { cout << "Key: " << p.first << ", Value: " << p.second << endl; } return 0; }
Key: 1, Value: Apple Key: 2, Value: Banana