std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: unordered_map
| (1) | ||
|
unordered_map
(
)
: unordered_map ( size_type ( /* 未指定 */ ) ) { } |
(自 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); // 范围构造函数 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()); // 选项1:使用自定义键类型的构造函数 // Define the KeyHash and KeyEqual structs and use them in the template std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 = { {{"John", "Doe"}, "示例"}, {{"Mary", "Sue"}, "另一个"} }; // 构造函数的第二种方案:使用自定义键类型。 // 为类/结构体定义 const == 运算符并特化 std::hash // structure in the std namespace std::unordered_map<Foo, std::string> m7 = { {Foo(1), "一"}, {2, "二"}, {3, "三"} }; // 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++标准。
另请参阅
| ||||||||||||||||||||||||||||||||||||||||||||||||||||