Namespaces
Variants

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

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
swap と 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通の語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
ヘッダ <utility>
template< class T1, class T2, class U1, class U2 >
bool operator==( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
で定義 (1) (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以降)
template< class T1, class T2, class U1, class U2 >
bool operator!=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(2) (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 >
bool operator<( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(3) (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 >
bool operator<=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(4) (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 >
bool operator>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(5) (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 >
bool operator>=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(6) (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 の戻り値の型です。

<, <=, >, >=, および != の各演算子は、それぞれ synthesized 演算子 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)
タプル内の値を辞書順で比較する
(関数テンプレート)