unordered_map_unordered_map
unordered_map_unordered_map (std::unordered_map::unordered_map — 생성자)
std::unordered_map 객체를 만드는 생성자들이에요. 기본 생성자부터 해시, 동등 비교, 버킷 개수, 이터레이터 범위, 초기화 목록, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.
출처: cppreference
본문
주요 시그니처는 다음과 같아요.
unordered_map();
explicit unordered_map( size_type bucket_count,
const Hash& hash = Hash(),
const key_equal& equal = key_equal(),
const Allocator& alloc = Allocator() ); // (2)
template< class InputIt >
unordered_map( InputIt first, InputIt last,
size_type bucket_count = /*implementation-defined*/,
const Hash& hash = Hash(),
const key_equal& equal = key_equal(),
const Allocator& alloc = Allocator() ); // (3)
unordered_map( std::initializer_list<value_type> init,
size_type bucket_count = /*implementation-defined*/,
const Hash& hash = Hash(),
const key_equal& equal = key_equal(),
const Allocator& alloc = Allocator() ); // (4) (since C++11)
unordered_map( const unordered_map& other ); // (5)
unordered_map( unordered_map&& other ); // (6) (since C++11)
template< container-compatible-range<value_type> R >
unordered_map( std::from_range_t, R&& rg,
size_type bucket_count = ..., const Hash& hash = Hash(),
const key_equal& equal = key_equal(),
const Allocator& alloc = Allocator() ); // (from_range)
여러 오버로드가 있는데, 핵심은 다음과 같아요.
- 기본 생성자: 빈 컨테이너를 만들어요.
- 버킷 개수 + 해시/동등/할당자 지정.
- 범위/초기화 목록/from_range 원소들로 만드는 생성자.
- 복사/이동 생성자.
unordered_map의 복잡도는 여러 생성자에 걸쳐 N개의 원소를 넣으면 평균 O(N)이에요.
예제
#include <iostream>
#include <unordered_map>
int main()
{
std::unordered_map<int, char> m{{1, 'a'}, {2, 'b'}}; // init list
std::unordered_map<int, char> m2(m); // 복사
std::cout << m2.size() << '\n'; // 2
}