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