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 이후) |
목차매개변수
복잡도
1-5)
상수.
9,10)
other
의 크기에 선형적입니다.
11,12)
상수. 만약
alloc
이 주어지고
alloc
!
=
other.
get_allocator
(
)
인 경우, 선형입니다.
예외
참고컨테이너 이동 생성(오버로드(4)) 후에, 원래 C++23까지 공식적으로 요구되지는 않지만, 일부 구현에서는 이미 이전 모드에서 템플릿 매개변수
예제
이 코드 실행
#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++ 표준에 소급 적용되었습니다.
참고 항목
| ||||||||||||||||||||||||||||||||||||||||||||||||||||