命名空间
变体
操作

operator==,!=,<,<=,>,>=,<=>(std::pair)

来自 cppreference.cn
< cpp‎ | utility‎ | pair
 
 
 
std::pair
成员函数
(C++11)
非成员函数
operator==operator!=operator<operator<=operator>operator>=operator<=>
(直到 C++20)(直到 C++20)(直到 C++20)(直到 C++20)(直到 C++20)(C++20)
辅助类
推导指引(C++17)
 
定义于头文件 <utility>
(1)
template< class T1, class T2, class U1, class U2 >
bool operator==( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator==( const std::pair<T1, T2>& lhs,

                           const std::pair<U1, U2>& rhs );
(自 C++14 起)
(2)
template< class T1, class T2, class U1, class U2 >
bool operator!=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator!=( const std::pair<T1, T2>& lhs,

                           const std::pair<U1, U2>& rhs );
(自 C++14 起)
(直到 C++20)
(3)
template< class T1, class T2, class U1, class U2 >
bool operator<( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator<( const std::pair<T1, T2>& lhs,

                          const std::pair<U1, U2>& rhs );
(自 C++14 起)
(直到 C++20)
(4)
template< class T1, class T2, class U1, class U2 >
bool operator<=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator<=( const std::pair<T1, T2>& lhs,

                           const std::pair<U1, U2>& rhs );
(自 C++14 起)
(直到 C++20)
(5)
template< class T1, class T2, class U1, class U2 >
bool operator>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator>( const std::pair<T1, T2>& lhs,

                          const std::pair<U1, U2>& rhs );
(自 C++14 起)
(直到 C++20)
(6)
template< class T1, class T2, class U1, class U2 >
bool operator>=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(直到 C++14)
template< class T1, class T2, class U1, class U2 >

constexpr bool operator>=( const std::pair<T1, T2>& lhs,

                           const std::pair<U1, U2>& rhs );
(自 C++14 起)
(直到 C++20)
template< class T1, class T2, class U1, class U2 >

constexpr std::common_comparison_category_t<synth-three-way-result<T1, U1>,
                                            synth-three-way-result<T2, U2>>

    operator<=>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(7) (自 C++20 起)
1,2) 测试 lhsrhs 的两个元素是否相等,即比较 lhs.firstrhs.first 以及 lhs.secondrhs.second

如果 lhs.first == rhs.firstlhs.second == rhs.second 的类型和值类别不满足 BooleanTestable 要求,则行为未定义。

(直到 C++26)

此重载仅在 decltype(lhs.first == rhs.first)decltype(lhs.second == rhs.second) 都符合 boolean-testable 模型时参与重载决议。

(自 C++26 起)
3-6) 通过 operator< 字典序比较 lhsrhs,即比较第一个元素,只有当它们相等时,才比较第二个元素。 如果 lhs.first < rhs.firstrhs.first < lhs.firstlhs.second < rhs.second 的类型和值类别不满足 BooleanTestable 要求,则行为未定义。
7) 通过 synth-three-way 字典序比较 lhsrhs,即比较第一个元素,只有当它们相等时,才比较第二个元素。synth-three-way-resultsynth-three-way 的返回类型。

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(自 C++20 起)

内容

[编辑] 参数

lhs, rhs - 要比较的 pair

[编辑] 返回值

1) 如果 lhs.first == rhs.firstlhs.second == rhs.second 都为 true,则返回 true,否则返回 false
2) !(lhs == rhs)
3) 如果 lhs.first < rhs.first,则返回 true。否则,如果 rhs.first < lhs.first,则返回 false。否则,如果 lhs.second < rhs.second,则返回 true。否则,返回 false
4) !(rhs < lhs)
5) rhs < lhs
6) !(lhs < rhs)
7) 如果 synth-three-way(lhs.first, rhs.first) 不等于 0,则返回 synth-three-way(lhs.second, rhs.second)

[编辑] 注解

关系运算符根据每个元素的 operator< 定义。

(直到 C++20)

关系运算符根据 synth-three-way 定义,后者尽可能使用 operator<=>,否则使用 operator<

值得注意的是,如果元素类型本身不提供 operator<=>,但可以隐式转换为三路可比较类型,则将使用该转换而不是 operator<

(自 C++20 起)
特性测试 Std 特性
__cpp_lib_constrained_equality 202403L (C++26) 用于 std::pair 的受约束 operator==

[编辑] 示例

由于为 pair 定义了 operator<,因此可以对 pair 的容器进行排序。

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
 
int main()
{
    std::vector<std::pair<int, std::string>> v = {{2, "baz"}, {2, "bar"}, {1, "foo"}};
    std::sort(v.begin(), v.end());
 
    for (auto p : v)
        std::cout << '{' << p.first << ", " << std::quoted(p.second) << "}\n";
}

输出

{1, "foo"}
{2, "bar"}
{2, "baz"}

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 296 C++98 缺少除 ==< 之外的运算符的描述 已添加
LWG 2114
(P2167R3)
C++98 缺少布尔运算的类型先决条件 已添加
LWG 3865 C++98 比较运算符仅接受相同类型的 pair 接受不同类型的 pair

[编辑] 参见

(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20)
字典序比较元组中的值
(函数模板) [编辑]