multimap_merge

multimap_merge (std::multimap::merge — 병합)

std::map이나 std::multimap의 원소들을 현재 멀티맵으로 이식(splice)하는 멤버 함수예요. 원소는 복사되지 않고 노드가 통째로 이동해요.

출처: cppreference

본문

시그니처는 다음과 같아요 (모두 since C++17).

template< class C2 >
void merge( std::map<Key, T, C2, Allocator>& source );      // (1)
template< class C2 >
void merge( std::map<Key, T, C2, Allocator>&& source );     // (2)
template< class C2 >
void merge( std::multimap<Key, T, C2, Allocator>& source ); // (3)
template< class C2 >
void merge( std::multimap<Key, T, C2, Allocator>&& source );// (4)

source의 각 원소를 추출("splice")해 *this의 비교자로 삽입해요. 멀티맵이므로 중복 키여도 남는 원소가 없고 source의 모든 원소가 이동돼요. *thissource에 동등한 키가 있으면 *this의 원소가 먼저 온 뒤 source의 원소가 이어져요.

노드가 이동될 뿐 복사·이동 연산은 일어나지 않아요. 노드 핸들 방식으로 옮기므로 재할당이 없어요.

매개변수

  • source: 원소를 이식할 호환되는 소스 컨테이너.

복잡도

N·log(size() + N). 여기서 Nsource.size()예요.

예제

#include <iostream>
#include <map>
int main()
{
    std::multimap<int, char> a{{1, 'a'}, {2, 'b'}};
    std::map<int, char> b{{2, 'x'}, {3, 'c'}};
    a.merge(b);   // b의 모든 원소가 a로 이동 (멀티맵이므로 2 중복 허용)
    for (auto& [k, v] : a) std::cout << k << ':' << v << ' ';
    // 1:a 2:b 2:x 3:c
}

더 알아보기 (Learn more)

cppreference