operator==,!=,<,<=,>,>=,<=>(std::pair)
<utility>
bool operator==( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator==( const std::pair <T1, T2>& lhs,
bool operator!=( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator!=( const std::pair <T1, T2>& lhs,
(until C++20)
bool operator<( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator<( const std::pair <T1, T2>& lhs,
(until C++20)
bool operator<=( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator<=( const std::pair <T1, T2>& lhs,
(until C++20)
bool operator>( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator>( const std::pair <T1, T2>& lhs,
(until C++20)
bool operator>=( const std::pair <T1, T2>& lhs, const std::pair <U1, U2>& rhs );
constexpr bool operator>=( const std::pair <T1, T2>& lhs,
(until C++20)
constexpr std::common_comparison_category_t <synth-three-way-result<T1, U1>,
synth-three-way-result<T2, U2>>
The behavior is undefined if the type and value category of either lhs.first == rhs.first or lhs.second == rhs.second do not meet the BooleanTestable requirements.
(until C++26)This overload participates in overload resolution only if both decltype(lhs.first == rhs.first) and decltype(lhs.second == rhs.second) model boolean-testable
.
synth-three-way
.The <
, <=
, >
, >=
, and !=
operators are synthesized from operator<=> and operator== respectively.
[edit] Parameters
[edit] Return value
[edit] Notes
The relational operators are defined in terms of each element's operator<.
(until C++20)The relational operators are defined in terms of synth-three-way, which uses operator<=> if possible, or operator< otherwise.
Notably, if an element type does not itself provide operator<=>, but is implicitly convertible to a three-way comparable type, that conversion will be used instead of operator<.
(since C++20)Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | Constrained operator== for std::pair |
[edit] Example
Because operator< is defined for pairs, containers of pairs can be sorted.
#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"; }
Output:
{1, "foo"} {2, "bar"} {2, "baz"}
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 296 | C++98 | the descriptions of operators other than == and < were missing
|
added |
LWG 2114 (P2167R3) |
C++98 | type preconditions for boolean operations were missing | added |
LWG 3865 | C++98 | comparison operators only accepted pair s of the same type
|
accept pair s of different types
|
[edit] See also
(function template) [edit]