cpp / latest / utility / pair / operator_cmp.html /

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

Defined in header <utility>
(1)
template< class T1, class T2 >
bool operator==( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator==( const std::pair<T1,T2>& lhs,
                           const std::pair<T1,T2>& rhs );
(since C++14)
(2)
template< class T1, class T2 >
bool operator!=( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator!=( const std::pair<T1,T2>& lhs,
                           const std::pair<T1,T2>& rhs );
(since C++14)
(until C++20)
(3)
template< class T1, class T2 >
bool operator<( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator<( const std::pair<T1,T2>& lhs,
                          const std::pair<T1,T2>& rhs );
(since C++14)
(until C++20)
(4)
template< class T1, class T2 >
bool operator<=( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator<=( const std::pair<T1,T2>& lhs,
                           const std::pair<T1,T2>& rhs );
(since C++14)
(until C++20)
(5)
template< class T1, class T2 >
bool operator>( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator>( const std::pair<T1,T2>& lhs,
                          const std::pair<T1,T2>& rhs );
(since C++14)
(until C++20)
(6)
template< class T1, class T2 >
bool operator>=( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );
(until C++14)
template< class T1, class T2 >
constexpr bool operator>=( const std::pair<T1,T2>& lhs,
                           const std::pair<T1,T2>& rhs );
(since C++14)
(until C++20)
template< class T1, class T2 >
constexpr /* see below */ operator<=>( const std::pair<T1,T2>& lhs,
                                       const std::pair<T1,T2>& rhs );
(7) (since C++20)
1-2) Tests if both elements of lhs and rhs are equal, that is, compares lhs.first with rhs.first and lhs.second with rhs.second.
3-6) Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements.
7) Compares lhs and rhs lexicographically by synthesized three-way comparison (see below), that is, compares the first elements and only if they are equivalent, compares the second elements.
The return type is the common comparison category type of the result type of synthesized three-way comparison on T1 and the one of T2.

The <, <=, >, >=, and != operators are synthesized from operator<=> and operator== respectively.

(since C++20)

Synthesized three-way comparison

Given an object type T, two const T lvalues lhs and rhs as left hand operand and right hand operand respectively, synthesized three-way comparison is defined as:

  • if std::three_way_comparable_with<T, T> is satisfied, equivalent to lhs <=> rhs;
  • otherwise, if comparing two const T lvalues by operator< is well-formed and the result type satisfies boolean-testable, equivalent to
lhs < rhs ? std::weak_ordering::less :
rhs < lhs ? std::weak_ordering::greater :
            std::weak_ordering::equivalent
  • otherwise, synthesized three-way comparison is not defined, which makes operator<=> not participate in overload resolution.

The behavior of operator<=> is undefined if three_way_comparable_with or boolean-testable is satisfied but not modeled.

(since C++20)

Parameters

lhs, rhs - pairs to compare

Return value

1) true if both lhs.first == rhs.first and lhs.second == rhs.second, otherwise false
2) !(lhs == rhs)
3) If lhs.first<rhs.first, returns true. Otherwise, if rhs.first<lhs.first, returns false. Otherwise, if lhs.second<rhs.second, returns true. Otherwise, returns false.
4) !(rhs < lhs)
5) rhs < lhs
6) !(lhs < rhs)
7) synth_three_way(lhs.first, rhs.first) if it is not equal to 0, otherwise synth_three_way(lhs.second, rhs.second), where synth_three_way is an exposition-only function object performing synthesized three-way comparison.

Example

Because operator< is defined for pairs, containers of pairs can be sorted.

#include <iostream>
#include <iomanip>
#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 << ", " << std::quoted(p.second) << "}\n";
    }
}

Output:

{1, "foo"}
{2, "bar"}
{2, "baz"}

See also

(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
lexicographically compares the values in the tuple
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/pair/operator_cmp