C++ pair 详解:用法、原理与实战示例
1. pair 简介std::pair是 C 标准库中定义在utility头文件中的一个模板类用于将两个值组合成一个单一对象。它提供了一种简单的方式来存储和操作两个相关联的值这两个值可以是相同类型也可以是不同类型。2. 基本用法2.1 创建 pair创建pair的几种常见方式#include iostream #include utility #include string int main() { // 方式1使用构造函数 std::pairint, std::string p1(1, apple); // 方式2使用 make_pair 函数自动推导类型 auto p2 std::make_pair(3.14, pi); // 方式3使用初始化列表C11及以上 std::pairint, double p3 {42, 3.14159}; // 方式4默认构造函数 std::pairstd::string, int p4; p4.first score; p4.second 100; return 0; }2.2 访问元素通过first和second成员访问pair的两个元素#include iostream #include utility int main() { std::pairint, std::string student(101, Alice); std::cout ID: student.first std::endl; // 输出: ID: 101 std::cout Name: student.second std::endl; // 输出: Name: Alice // 修改元素值 student.first 102; student.second Bob; return 0; }3. pair 的操作3.1 比较操作pair支持比较运算符,!,,,,按字典序比较先比较first如果相等再比较second。#include iostream #include utility int main() { std::pairint, int p1(1, 2); std::pairint, int p2(1, 3); std::pairint, int p3(2, 1); std::cout std::boolalpha; std::cout p1 p2: (p1 p2) std::endl; // true (11, 23) std::cout p1 p3: (p1 p3) std::endl; // true (12) std::cout p1 p1: (p1 p1) std::endl; // true return 0; }3.2 结构化绑定C17C17 引入了结构化绑定可以更方便地解构pair#include iostream #include utility #include map int main() { std::pairint, std::string p(100, hello); // 结构化绑定 auto [id, message] p; std::cout id : message std::endl; // 输出: 100: hello // 在范围for循环中使用 std::mapint, std::string m {{1, one}, {2, two}}; for (const auto [key, value] : m) { std::cout key value std::endl; } return 0; }4. 实战应用4.1 作为函数返回值pair常用于函数返回两个值#include iostream #include utility #include cmath // 返回二次方程的根和判别式 std::pairbool, std::pairdouble, double solveQuadratic(double a, double b, double c) { double discriminant b * b - 4 * a * c; if (discriminant 0) { return {false, {0, 0}}; // 无实根 } double sqrt_disc std::sqrt(discriminant); double x1 (-b sqrt_disc) / (2 * a); double x2 (-b - sqrt_disc) / (2 * a); return {true, {x1, x2}}; } int main() { auto [has_solution, roots] solveQuadratic(1, -3, 2); if (has_solution) { std::cout Roots: roots.first , roots.second std::endl; } else { std::cout No real roots std::endl; } return 0; }4.2 在 STL 容器中使用pair是std::map、std::unordered_map等容器的元素类型#include iostream #include map #include vector #include algorithm int main() { // map 的 value_type 就是 pairconst Key, T std::mapstd::string, int scores {{Alice, 95}, {Bob, 88}, {Charlie, 92}}; // 遍历 map for (const auto [name, score] : scores) { std::cout name : score std::endl; } // vector 存储 pair std::vectorstd::pairint, std::string items; items.push_back({1, apple}); items.emplace_back(2, banana); items.emplace_back(3, orange); // 按 first 排序 std::sort(items.begin(), items.end()); return 0; }4.3 自定义比较函数可以使用 lambda 表达式或函数对象自定义pair的比较逻辑#include iostream #include vector #include algorithm #include utility int main() { std::vectorstd::pairstd::string, int students { {Alice, 85}, {Bob, 92}, {Charlie, 78}, {David, 95} }; // 按分数降序排序 std::sort(students.begin(), students.end(), [](const auto a, const auto b) { return a.second b.second; // 按 second分数降序 }); // 输出排序结果 for (const auto [name, score] : students) { std::cout name : score std::endl; } return 0; }5. 注意事项头文件使用std::pair需要包含utility头文件。类型推导std::make_pair可以自动推导类型但要注意引用类型的处理。性能pair是轻量级结构通常按值传递但包含大对象时考虑按引用传递。C11 特性初始化列表和结构化绑定需要 C11 及以上标准支持。tuple如果需要组合两个以上的值可以使用std::tuple。6. 总结std::pair是 C 中处理成对数据的标准工具广泛应用于函数返回值、容器元素、算法参数等场景。掌握pair的使用能够提高代码的简洁性和可读性特别是在现代 C 中结合结构化绑定等特性能让代码更加优雅。