【C++ STL】 容器详解:pair 学习
在 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> pair 自己是一个固定大小的结构,只能存储 两个元素(first 和 second),因此不能直接插入多个元素。
#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 的区别
特性pairtuple元素个数2恣意个访问方式.first .secondget<n>(tuple)适用场景键值对、映射关系多个不同类型的数据 使用 tuple
tuple 是 C++ 标准库中的一个模板类,可以存储多个元素(数量不限)。它的用法雷同于pair,但可以容纳更多的元素。
#include <iostream>
#include <tuple> // 包含 std::tuple 的头文件
using namespace std;
int main() {
// 创建一个包含 3 个元素的 tuple
tuple<int, string, double> t1(1, "Apple", 3.14);
// 访问 tuple 的元素
cout << "ID: " << get<0>(t1) << ", Name: " << get<1>(t1) << ", Price: " << get<2>(t1) << endl;
return 0;
} 如果你需要存储多个键值对,可以使用 map 或 unordered_map。
#include <iostream>
#include <map>
using namespace std;
int main() {
// 创建一个 map,存储多个键值对
map<int, string> fruitMap = {
{1, "Apple"},
{2, "Banana"},
{3, "Cherry"}
};
// 插入新的键值对
fruitMap.insert(make_pair(4, "Durian"));
// 输出所有键值对
for (const auto& entry : fruitMap) {
cout << "Key: " << entry.first << ", Value: " << entry.second << endl;
}
return 0;
}
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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]