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 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) 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) lhs rhs operator < で辞書順比較します。つまり、最初の要素を比較し、それらが等価である場合にのみ2番目の要素を比較します。 lhs. first < rhs. first rhs. first < lhs. first 、または lhs. second < rhs. second の型と値カテゴリが BooleanTestable の要件を満たさない場合、動作は未定義です。
7) lhs rhs synth-three-way によって辞書順で比較します。つまり、最初の要素を比較し、それらが等価である場合にのみ、2番目の要素を比較します。 synth-three-way-result synth-three-way の戻り値の型です。

< <= > >= 、および != 演算子は、それぞれ 合成されます operator <=> および operator == から。

(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) 制約付き operator == std::pair に追加

operator < がペアに対して定義されているため、ペアのコンテナをソートすることができます。

#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)
タプル内の値を辞書順に比較する
(関数テンプレート)