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 起) |
参数
alloc | - | 用于此容器所有内存分配器的分配器 |
bucket_count | - | 初始化时用的最小桶数。若不指定,则使用实现定义的默认值 |
hash | - | 要用的散列函数 |
equal | - | 用于进行此容器所有的键比较的比较函数 |
first, last | - | 复制元素来源的范围 |
rg | - | 容器兼容范围,即其元素可以转换为 value_type 的 input_range
|
other | - | 用作源以初始化容器元素的另一容器 |
init | - | 用以初始化容器元素的 initializer_list |
类型要求 | ||
-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; // 列表构造函数 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()); // 带自定义 Key 类型的构造函数的选项 1 // 定义 KeyHash 与 KeyEqual 结构体并在模板中使用它们 std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 = { {{"John", "Doe"}, "example"}, {{"Mary", "Sue"}, "another"} }; // 带自定义 Key 类型的构造函数的选项 2 // 为 class/struct 定义 const == 运算符并于 std 命名空间特化 std::hash 结构体 std::unordered_map<Foo, std::string> m7 = { {Foo(1), "One"}, {2, "Two"}, {3, "Three"} }; // 选项 3:用 lambda // 注意必须将初始桶数传递给构造函数 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++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2193 | C++11 | 默认构造函数为 explicit | 使之为非 explicit |
参阅
将值赋给容器 (公开成员函数) |