operator==,!=,<,<=,>,>=(std::pair)
提供: cppreference.com
ヘッダ <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)
lhs
と rhs
の両方の要素が等しいかどうか調べます。 つまり、 lhs.first
と rhs.first
が等しく、かつ lhs.second
と rhs.second
が等しいかどうかを調べます。3-6)
lhs
と rhs
を辞書的に比較します。 つまり、 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 のコンテナはソートすることができます。
Run this code
#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)