Spec-Zone.ru › C++

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

Определено в заголовке <utility>
(1)
template< class T1, class T2, class U1, class U2 >
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 >
constexpr bool operator==( const std::pair<T1, T2>& lhs,
                           const std::pair<U1, U2>& rhs );
(с C++14)
(2)
template< class T1, class T2, class U1, class U2 >
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 >
constexpr bool operator!=( const std::pair<T1, T2>& lhs,
                           const std::pair<U1, U2>& rhs );
(с C++14)
(до C++20)
(3)
template< class T1, class T2, class U1, class U2 >
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 >
constexpr bool operator<( const std::pair<T1, T2>& lhs,
                          const std::pair<U1, U2>& rhs );
(с C++14)
(до C++20)
(4)
template< class T1, class T2, class U1, class U2 >
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 >
constexpr bool operator<=( const std::pair<T1, T2>& lhs,
                           const std::pair<U1, U2>& rhs );
(с C++14)
(до C++20)
(5)
template< class T1, class T2, class U1, class U2 >
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 >
constexpr bool operator>( const std::pair<T1, T2>& lhs,
                          const std::pair<U1, U2>& rhs );
(с C++14)
(до C++20)
(6)
template< class T1, class T2, class U1, class U2 >
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 >
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)
1,2) Проверяет, равны ли оба элемента lhs и rhs, то есть сравнивает lhs.first с rhs.first и lhs.second с rhs.second.
3-6) Сравнивает lhs и rhs лексикографически по operator<, то есть сравнивает первые элементы, и только если они эквивалентны, сравнивает вторые элементы.
7) Сравнивает lhs и rhs лексикографически по synth-three-way, то есть сравнивает первые элементы, и только если они эквивалентны, сравнивает вторые элементы. synth-three-way-result — это тип возвращаемого значения synth-three-way.

Операторы <, <=, >, >=, и != синтезируются из операторов <=> и == соответственно.

(с C++20)

Параметры

lhs, rhs - пары для сравнения

Значение возврата

1) true если оба lhs.first == rhs.first и lhs.second == rhs.second, в противном случае 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)
7) synth-three-way(lhs.first, rhs.first) если оно не равно ​0​, в противном случае synth-three-way(lhs.second, rhs.second).

Пример

Поскольку для пар определён оператор <, контейнеры пар можно сортировать.

#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 3865 C++98 операторы сравнения принимали только pair одного типа принимают pair разных типов

См. также

operator==operator!=operator<operator<=operator>operator>=operator<=>
(удалено в C++20)(удалено в C++20)(удалено в C++20)(удалено в C++20)(удалено в C++20)(C++20)
лексикографически сравнивает значения в кортеже
(шаблон функции)

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API