multimap_multimap
multimap_multimap (std::multimap::multimap — 생성자)
std::multimap 객체를 만드는 생성자들이에요. 기본 생성자부터 비교자, 이터레이터 범위, 초기화 목록, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.
출처: cppreference
본문
주요 시그니처는 다음과 같아요.
multimap();
explicit multimap( const Compare& comp, const Allocator& alloc = Allocator() ); // (2)
explicit multimap( const Allocator& alloc ); // (3) (since C++11)
template< class InputIt >
multimap( InputIt first, InputIt last,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (4)
multimap( std::initializer_list<value_type> init,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (5) (since C++11)
multimap( const multimap& other ); // (6)
multimap( multimap&& other ); // (7) (since C++11)
template< container-compatible-range<value_type> R >
multimap( std::from_range_t, R&& rg,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (from_range)
여러 오버로드가 있는데, 핵심은 다음과 같아요.
- 기본 생성자: 빈 컨테이너를 만들어요. C++11부터
multimap(Compare())으로 위임해요. - 비교자/할당자만 받는 생성자: 지정한 비교자와 할당자로 빈 컨테이너를 만들어요.
- (4) 범위
[first, last)의 원소들을 가진 컨테이너를 만들어요. - (5) 초기화 목록
init의 원소들을 가진 컨테이너를 만들어요. - (6)(7) 복사/이동 생성자.
- from_range 생성자: 범위
rg의 원소들을 가진 컨테이너를 만들어요.
멀티맵은 중복 키를 허용하므로, 정렬할 때 같은 키를 병합하지 않아요. multimap의 복잡도는 여러 생성자에 걸쳐 트리 구성이 필요하면 N·log(N) (N은 원소 개수)예요.
예제
#include <iostream>
#include <map>
int main()
{
std::multimap<int, char> m{{1, 'a'}, {1, 'b'}}; // 초기화 목록, 중복 허용
for (auto& [k, v] : m) std::cout << k << ':' << v << ' ';
}