Namespaces
Variants

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

From cppreference.net
Utilities library
定义于头文件 <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
1,2) 测试 lhs rhs 的两个元素是否均相等,即比较 lhs. first rhs. first ,以及 lhs. second rhs. second

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

(C++26 前)

仅当 decltype ( lhs. first == rhs. first ) decltype ( lhs. second == rhs. second ) 均满足 boolean-testable 概念时,此重载才参与重载决议。

(C++26 起)
3-6) 通过 operator < 按字典序比较 lhs rhs ,即先比较首元素,仅当它们等价时再比较次元素。若 lhs. first < rhs. first rhs. first < lhs. first lhs. second < rhs. second 中任意表达式的类型和值类别不满足 BooleanTestable 要求,则行为未定义。
7) 通过 synth-three-way lhs rhs 进行字典序比较,即先比较首元素,仅当它们等价时才比较次元素。 synth-three-way-result synth-three-way 的返回类型。

< <= > >= != 运算符分别由 operator <=> operator == 自动合成。

(since C++20)

目录

参数

lhs, rhs - 待比较的配对参数

返回值

1) true 当且仅当 lhs. first == rhs. first lhs. second == rhs. second ,否则为 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 起)
功能测试 标准 功能特性
__cpp_lib_constrained_equality 202403L (C++26) std::pair 的约束化 operator ==

示例

由于为 operator < 定义了针对 pair 的实现,因此包含 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++ 标准。

缺陷报告 适用范围 发布时行为 正确行为
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)
按字典序比较元组中的值
(函数模板)