unordered_map_operator_cmp

unordered_map_operator_cmp (std::unordered_map::operator==,!= — 비교 연산자)

std::unordered_map을 비교하는 비멤버 연산자 함수들이에요. 원소 순서는 무관하게 동등성을 판정해요.

출처: cppreference

본문

시그니처는 다음과 같아요.

template< class Key, class T, class Hash, class KeyEqual, class Alloc >
bool operator==( const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& lhs,
                 const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& rhs );

두 컨테이너를 비교해요. unordered_mapoperator==는 키·값 쌍의 다중집합(multiset)이 같은지, 즉 각 키가 같은 개수만큼 존재하고 같은 값에 매핑되는지로 판정해요. 버킷(내부) 순서는 무관해요. C++20부터 operator!===의 부정으로 파생돼요.

(참고: unordered_map에는 operator<, operator<=> 같은 순서 비교가 없어요).

반환값

두 맵이 같은 키-값 쌍을 담고 있으면 true.

복잡도

c1.size() == c2.size()일 때 N·(평균 상수) (N은 원소 수). 크기가 다르면 즉시 false.

예제

#include <iostream>
#include <unordered_map>
int main()
{
    std::unordered_map<int, char> a{{1, 'a'}, {2, 'b'}};
    std::unordered_map<int, char> b{{2, 'b'}, {1, 'a'}};   // 순서 다름
    std::cout << std::boolalpha << (a == b) << '\n';   // true
}

더 알아보기 (Learn more)

cppreference