Namespaces
Variants

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

From cppreference.net
Utilities library
헤더에 정의됨 <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 를 비교합니다.

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)
3-6) lhs rhs operator < 로 사전식 비교합니다. 즉, 첫 번째 요소들을 비교하고 동등할 경우에만 두 번째 요소들을 비교합니다. lhs. first < rhs. first , rhs. first < lhs. first , 또는 lhs. second < rhs. second 중 어느 하나의 타입과 값 카테고리가 BooleanTestable 요구사항을 충족하지 않을 경우 동작은 정의되지 않습니다.
7) lhs rhs synth-three-way 를 사용하여 사전식으로 비교합니다. 즉, 첫 번째 요소들을 비교하고 동등한 경우에만 두 번째 요소들을 비교합니다. synth-three-way-result synth-three-way 의 반환 타입입니다.

< , <= , > , >= , != 연산자들은 각각 operator <=> operator == 로부터 합성됩니다 .

(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 ) 를 반환합니다.

참고 사항

관계 연산자는 각 요소의 operator < 를 기반으로 정의됩니다.

(C++20까지)

관계 연산자는 synth-three-way 를 기반으로 정의되며, 이는 가능한 경우 operator <=> 를 사용하고, 그렇지 않으면 operator < 를 사용합니다.

특히, 요소 타입 자체가 operator <=> 를 제공하지 않지만 three-way 비교 가능 타입으로 암시적으로 변환 가능한 경우, operator < 대신 해당 변환이 사용됩니다.

(C++20부터)
기능 테스트 매크로 표준 기능
__cpp_lib_constrained_equality 202403L (C++26) 제약된 operator == for std::pair

예제

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 를 허용함

참고 항목

(C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20)
튜플 내의 값들을 사전식으로 비교
(함수 템플릿)