名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | utility‎ | pair
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
std::pair
メンバ関数
非メンバ関数
operator==operator!=operator<operator<=operator>operator>=operator<=>
(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20)
(C++11)
推定ガイド(C++17)
ヘルパークラス
(C++11)
 
ヘッダ <utility> で定義
template< class T1, class T2 >
bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(1) (C++14未満)
template< class T1, class T2 >
constexpr bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(1) (C++14以上)
template< class T1, class T2 >
bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(2) (C++14未満)
template< class T1, class T2 >
constexpr bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(2) (C++14以上)
template< class T1, class T2 >
bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(3) (C++14未満)
template< class T1, class T2 >
constexpr bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(3) (C++14以上)
template< class T1, class T2 >
bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(4) (C++14未満)
template< class T1, class T2 >
constexpr bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(4) (C++14以上)
template< class T1, class T2 >
bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(5) (C++14未満)
template< class T1, class T2 >
constexpr bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(5) (C++14以上)
template< class T1, class T2 >
bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(6) (C++14未満)
template< class T1, class T2 >
constexpr bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
(6) (C++14以上)
1-2) lhsrhs の両方の要素が等しいかどうか調べます。 つまり、 lhs.firstrhs.first が等しく、かつ lhs.secondrhs.second が等しいかどうかを調べます。
3-6) lhsrhs を辞書的に比較します。 つまり、 first 要素を比較し、それが等しい場合に限り、 second 要素を比較します。

[編集] 引数

lhs, rhs - 比較するペア

[編集] 戻り値

1) lhs.first == rhs.first および lhs.second == rhs.second がどちらも成立すれば 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) を返します。

[編集]

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

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
 
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 << "," << p.second << ")\n";
    }
}

出力:

(1,foo)
(2,bar)
(2,baz)