pair_operator_cmp

pair_operator_cmp (pair 비교 연산자)

std::pair의 비교 연산자들을 다루는 페이지예요. 이 연산자들은 두 pair를 사전식(lexicographically)으로 비교해서 bool 값을 돌려줘요. C++20부터는 우주선 연산자(<=>)도 제공돼요.

출처: cppreference

본문

정의

헤더 에 정의됨
template < class T1 , class T2 , class U1 , class U2 > bool operator == ( const std :: pair < T1 , T2 >& lhs , const std :: pair < U1 , U2 >& rhs ); (1) (until 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 ); (since 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) (until 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 ); (since C++14) (until 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) (until 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 ); (since C++14) (until 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) (until 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 ); (since C++14) (until 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) (until 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 ); (since C++14) (until 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) (until 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 ); (since C++14) (until 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) (since C++20)
lhs . first == rhs . first 또는 lhs . second == rhs . second의 타입과 값 범주가 BooleanTestable 요구 사항을 충족하지 않으면 동작이 정의되지 않아요. (until C++26)
이 오버로드는 decltype ( lhs . first == rhs . first )와 decltype ( lhs . second == rhs . second )가 모두 boolean-testable 모델일 때만 오버로드 해석에 참여해요. (since C++26)
<, <=, >, >=, != 연산자는 각각 operator<=>와 operator==로부터 합성돼요. (since C++20)

매개변수

lhs, rhs - 비교할 pair들

반환값

해당 비교가 성립하면 true, 아니면 false를 반환해요.

참고

관계 연산자는 각 요소의 operator<를 기준으로 정의돼요. (until C++20)
관계 연산자는 synth-three-way를 기준으로 정의돼요. synth-three-way는 가능하면 operator<=>를 사용하고, 그렇지 않으면 operator<를 사용해요. 특히 요소 타입이 자체적으로 operator<=>를 제공하지 않지만, 삼방향 비교 가능한 타입으로 암시적으로 변환될 수 있다면 operator< 대신 그 변환이 사용돼요. (since C++20)

기능 테스트 매크로

Feature-test macro 표준 기능
__cpp_lib_constrained_equality 202403L (C++26) std::pair에 대한 제약된 operator==

예제

operator<가 pair에 대해 정의되어 있으므로, pair의 컨테이너를 정렬할 수 있어요.

#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도 받아들임

같이 보기

operator== operator!= operator< operator<= operator> operator>= operator<=> (removed in C++20) (removed in C++20) (removed in C++20) (removed in C++20) (removed in C++20) (C++20) tuple의 값을 사전식으로 비교해요 (함수 템플릿) [edit]

더 알아보기 (Learn more)

cppreference