std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: unordered_map
| (1) | ||
|
unordered_map
(
)
: unordered_map ( size_type ( /* unspecified */ ) ) { } |
(C++11부터)
(C++20까지) |
|
|
unordered_map
(
)
;
|
(C++20부터) | |
|
explicit
unordered_map
(
size_type bucket_count,
const
Hash
&
hash
=
Hash
(
)
,
|
(2) | (C++11 이후) |
|
unordered_map
(
size_type bucket_count,
const
Allocator
&
alloc
)
|
(3) | (C++14 이후) |
|
unordered_map
(
size_type bucket_count,
const
Hash
&
hash,
|
(4) | (C++14 이후) |
|
explicit
unordered_map
(
const
Allocator
&
alloc
)
;
|
(5) | (C++11 이후) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(6) | (C++11 이후) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(7) | (C++14 이후) |
|
template
<
class
InputIt
>
unordered_map
(
InputIt first, InputIt last,
|
(8) | (C++14부터) |
|
unordered_map
(
const
unordered_map
&
other
)
;
|
(9) | (C++11 이후) |
|
unordered_map
(
const
unordered_map
&
other,
const
Allocator
&
alloc
)
;
|
(10) | (C++11 이후) |
|
unordered_map
(
unordered_map
&&
other
)
;
|
(11) | (C++11 이후) |
|
unordered_map
(
unordered_map
&&
other,
const
Allocator
&
alloc
)
;
|
(12) | (C++11 이후) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count
=
/* 지정되지 않음 */
,
|
(13) | (C++11 이후) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count,
|
(14) | (C++14부터) |
|
unordered_map
(
std::
initializer_list
<
value_type
>
init,
size_type bucket_count,
|
(15) | (C++14 이후) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(16) | (C++23부터) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(17) | (C++23부터) |
|
template
<
container-compatible-range
<
value_type
>
R
>
unordered_map
(
std::
from_range_t
, R
&&
rg,
|
(18) | (C++23부터) |
다양한 데이터 소스로부터 새로운 컨테이너를 생성합니다. 선택적으로 사용자가 제공한 bucket_count 를 생성할 최소 버킷 수로 사용하고, hash 를 해시 함수로, equal 를 키 비교 함수로, alloc 를 할당자로 사용합니다.
[
first
,
last
)
의 내용으로 컨테이너를 생성합니다.
max_load_factor()
를
1.0
으로 설정합니다. 범위 내에 동등한 키를 가진 여러 요소가 있는 경우, 어떤 요소가 삽입될지 명시되지 않습니다 (
LWG2844
보류 중).
|
템플릿 매개변수
|
(C++23 이후) |
|
템플릿 매개변수
|
(C++23 이후) |
목차 |
매개변수
| alloc | - | 이 컨테이너의 모든 메모리 할당에 사용할 할당자 |
| bucket_count | - | 초기화 시 사용할 최소 버킷 수. 지정되지 않으면 명시되지 않은 기본값이 사용됨 |
| hash | - | 사용할 해시 함수 |
| equal | - | 이 컨테이너의 모든 키 비교에 사용할 비교 함수 |
| first, last | - | 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍 |
| rg | - |
컨테이너 호환 범위
, 즉 요소들이
value_type
으로 변환 가능한
input_range
|
| other | - | 컨테이너의 요소들을 초기화하는 데 사용할 소스로 사용할 다른 컨테이너 |
| init | - | 컨테이너의 요소들을 초기화하는 데 사용할 초기화자 리스트 |
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
요구사항을 충족해야 함
|
||
복잡도
예외
Allocator::allocate
에 대한 호출은 예외를 발생시킬 수 있습니다.
참고 사항
C++23까지 공식적으로 요구되지는 않지만, 일부 구현에서는 이미 이전 모드에서 템플릿 매개변수
Allocator
를
비추론 문맥
에 배치한 경우가 있습니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | 범위 인식 생성 및 삽입; 오버로드 ( 16-18 ) |
예제
#include <bitset> #include <string> #include <unordered_map> #include <utility> #include <vector> struct Key { std::string first; std::string second; }; struct KeyHash { std::size_t operator()(const Key& k) const { return std::hash<std::string>()(k.first) ^ (std::hash<std::string>()(k.second) << 1); } }; struct KeyEqual { bool operator()(const Key& lhs, const Key& rhs) const { return lhs.first == rhs.first && lhs.second == rhs.second; } }; struct Foo { Foo(int val_) : val(val_) {} int val; bool operator==(const Foo &rhs) const { return val == rhs.val; } }; template<> struct std::hash<Foo> { std::size_t operator()(const Foo &f) const { return std::hash<int>{}(f.val); } }; int main() { // 기본 생성자: 빈 맵 std::unordered_map<std::string, std::string> m1; // list constructor std::unordered_map<int, std::string> m2 = { {1, "foo"}, {3, "bar"}, {2, "baz"} }; // 복사 생성자 std::unordered_map<int, std::string> m3 = m2; // 이동 생성자 std::unordered_map<int, std::string> m4 = std::move(m2); // range constructor std::vector<std::pair<std::bitset<8>, int>> v = {{0x12, 1}, {0x01,-1}}; std::unordered_map<std::bitset<8>, double> m5(v.begin(), v.end()); // 사용자 정의 Key 타입을 위한 생성자 옵션 1 // KeyHash 및 KeyEqual 구조체를 정의하고 템플릿에서 사용 std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 = { {{"존", "Doe"}, "example"}, {{"Mary", "Sue"}, "다른"} }; // 사용자 정의 Key 타입을 사용하는 생성자를 위한 옵션 2. // 클래스/구조체에 대한 const == 연산자를 정의하고 std::hash를 특수화합니다 // std 네임스페이스 내의 구조체 std::unordered_map<Foo, std::string> m7 = { {Foo(1), "하나"}, {2, "Two"}, {3, "Three"} }; // Option 3: Use lambdas // 초기 버킷 수는 생성자에 전달되어야 함 struct Goo { int val; }; auto hash = [](const Goo &g){ return std::hash<int>{}(g.val); }; auto comp = [](const Goo &l, const Goo &r){ return l.val == r.val; }; std::unordered_map<Goo, double, decltype(hash), decltype(comp)> m8(10, hash, comp); }
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2193 | C++11 | 기본 생성자 ( 1 ) 가 explicit였음 | non-explicit로 변경됨 |
| LWG 2230 | C++11 | 오버로드 ( 13 ) 의 의미론이 명시되지 않았음 | 명시됨 |
참고 항목
|
컨테이너에 값을 할당합니다
(public member function) |