operator==,!=,<,<=,>,>=,<=>(std::pair)
| ヘッダ <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以降) |
lhs と rhs の両方の要素が等しいかどうかを調べます。つまり、lhs.first と rhs.first、および lhs.second と rhs.second を比較します。|
|
(C++26まで) |
|
このオーバーロードは、 |
(C++26以降) |
lhs と rhs を operator< によって辞書順に比較します。つまり、最初の要素を比較し、それらが等価である場合にのみ2番目の要素を比較します。lhs.first < rhs.first, rhs.first < lhs.first, または lhs.second < rhs.second のいずれかの型と値カテゴリが BooleanTestable 要件を満たさない場合、動作は未定義です。lhs と rhs を synth-three-way によって辞書順に比較します。つまり、最初の要素を比較し、それらが等価である場合にのみ2番目の要素を比較します。 synth-three-way-result は synth-three-way の戻り値の型です。|
|
(C++20以降) |
パラメータ
| lhs, rhs | - | 比較対象のペア |
戻り値
注記
|
関係演算子は各要素の 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) |
タプル内の値を辞書順で比較する (関数テンプレート) |