unordered_map_merge
unordered_map_merge (std::unordered_map::merge — 병합)
std::unordered_map이나 std::unordered_multimap의 원소들을 현재 맵으로 이식(splice)하는 멤버 함수예요. 원소는 복사되지 않고 노드가 통째로 이동해요.
출처: cppreference
본문
시그니처는 다음과 같아요 (모두 since C++17).
template< class H2, class P2 >
void merge( std::unordered_map<Key, T, H2, P2, Allocator>& source ); // (1)
template< class H2, class P2 >
void merge( std::unordered_map<Key, T, H2, P2, Allocator>&& source ); // (2)
template< class H2, class P2 >
void merge( std::unordered_multimap<Key, T, H2, P2, Allocator>& source ); // (3)
template< class H2, class P2 >
void merge( std::unordered_multimap<Key, T, H2, P2, Allocator>&& source );// (4)
source의 각 원소를 추출("splice")해 *this에 삽입하려고 시도해요. *this에 이미 같은 키가 있으면 그 원소는 source에 남아요. 노드가 이동될 뿐 복사·이동 연산은 일어나지 않아요.
매개변수
source: 원소를 이식할 호환되는 소스 컨테이너.
복잡도
평균 상수(amortized constant), 최악 선형.
예제
#include <iostream>
#include <unordered_map>
int main()
{
std::unordered_map<int, char> a{{1, 'a'}, {2, 'b'}};
std::unordered_map<int, char> b{{2, 'x'}, {3, 'c'}};
a.merge(b); // 2는 이미 있어 b에 남음
std::cout << a.size() << '\n'; // 3
}