Namespaces
Variants

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

cppreference.net에서 가져옴
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
스왑 및 형식 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 형식
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
헤더 <utility>
template< class T1, class T2, class U1, class U2 >
bool operator==( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
에 정의됨 (1) (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부터)
template< class T1, class T2, class U1, class U2 >
bool operator!=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(2) (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 >
bool operator<( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(3) (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 >
bool operator<=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(4) (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 >
bool operator>( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(5) (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 >
bool operator>=( const std::pair<T1, T2>& lhs, const std::pair<U1, U2>& rhs );
(6) (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 요구 사항을 충족하지 않으면 동작이 정의되지 않습니다.

(C++26까지)

이 오버로드는 decltype(lhs.first == rhs.first)과 decltype(lhs.second == rhs.second)가 모두 boolean-testable을 모델링하는 경우에만 오버로드 해결에 참여합니다.

(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의 반환 형식입니다.

<, <=, >, >= 및 != 연산자는 각각 synthesized from operator<=> and operator== respectively.

(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)
튜플의 값을 사전식으로 비교합니다
(함수 템플릿)