std::unordered_map
定义于头文件 <unordered_map> |
||
template< class Key, |
(1) | (since C++11) |
namespace pmr { template< |
(2) | (since C++17) |
std::unordered_map
是一个关联容器,包含键值对,其中键是唯一的。元素的搜索、插入和移除操作平均时间复杂度为常数。
在内部,元素不以任何特定顺序排序,而是组织成桶(buckets)。元素被放入哪个桶完全取决于其键的哈希值。具有相同哈希码的键会出现在同一个桶中。这允许快速访问单个元素,因为一旦计算出哈希值,它就指向元素所在的精确桶。
当传递两个键时,如果映射的键相等谓词返回 true,则认为这两个键等价。如果两个键等价,则哈希函数必须为这两个键返回相同的值。
std::unordered_map
满足 Container、 AllocatorAwareContainer、 UnorderedAssociativeContainer 的要求。
|
(since C++26) |
目录 |
[编辑] 迭代器失效
操作 | 失效情况 |
---|---|
所有只读操作、 swap、 std::swap | 从不 |
clear、 rehash、 reserve、 operator= | 总是 |
insert、 emplace、 emplace_hint、 operator[] | 仅当引起重哈希时 |
erase | 仅对被擦除的元素失效 |
[编辑] 注释
- swap 函数不会使容器内的任何迭代器失效,但它们会使标记交换区域末尾的迭代器失效。
- 即使在相应的迭代器失效时,容器中存储的键或数据的引用和指针也仅在擦除该元素时才会失效。
[编辑] 模板参数
本节尚不完整 原因:添加模板参数的描述。 |
[编辑] 成员类型
类型 | 定义 |
key_type
|
Key |
mapped_type
|
T |
value_type
|
std::pair<const Key, T> |
size_type
|
无符号整数类型(通常为 std::size_t) |
difference_type
|
有符号整数类型(通常为 std::ptrdiff_t) |
hasher
|
Hash |
key_equal
|
KeyEqual |
allocator_type
|
Allocator |
reference
|
value_type& |
const_reference
|
const value_type& |
pointer
|
std::allocator_traits<Allocator>::pointer |
const_pointer
|
std::allocator_traits<Allocator>::const_pointer |
iterator
|
LegacyForwardIterator 和 ConstexprIterator(since C++26) 指向 value_type |
const_iterator
|
LegacyForwardIterator 和 ConstexprIterator(since C++26) 指向 const value_type |
local_iterator
|
一种迭代器类型,其类别、值、差值、指针和 引用类型与 iterator 相同。此迭代器可用于迭代单个桶,但不能跨桶迭代 |
const_local_iterator
|
一种迭代器类型,其类别、值、差值、指针和 引用类型与 const_iterator 相同。此迭代器可用于迭代单个桶,但不能跨桶迭代 |
node_type (since C++17) |
表示容器节点的 node handle 的特化 |
insert_return_type (since C++17) |
描述插入 node_type 结果的类型,是以下内容的特化:template<class Iter, class NodeType> |
[编辑] 成员函数
构造 unordered_map (public member function) | |
析构 unordered_map (public member function) | |
为容器赋值 (public member function) | |
返回关联的分配器 (public member function) | |
迭代器 | |
返回指向开始的迭代器 (public member function) | |
返回指向末尾的迭代器 (public member function) | |
容量 | |
检查容器是否为空 (public member function) | |
返回元素数量 (public member function) | |
返回最大可能元素数量 (public member function) | |
修改器 | |
清除内容 (public member function) | |
插入元素 或节点(since C++17) (public member function) | |
(C++23) |
插入元素范围 (public member function) |
(C++17) |
插入元素,如果键已存在则赋值给当前元素 (public member function) |
就地构造元素 (public member function) | |
使用提示就地构造元素 (public member function) | |
(C++17) |
如果键不存在则就地插入,如果键存在则不执行任何操作 (public member function) |
erases elements (public member function) | |
交换内容 (public member function) | |
(C++17) |
从容器中提取节点 (public member function) |
(C++17) |
从另一个容器拼接节点 (public member function) |
查找 | |
访问指定元素,带边界检查 (public member function) | |
访问或插入指定元素 (public member function) | |
返回匹配特定键的元素数量 (public member function) | |
查找具有特定键的元素 (public member function) | |
(C++20) |
检查容器是否包含具有特定键的元素 (public member function) |
返回匹配特定键的元素范围 (public member function) | |
桶接口 | |
返回指向指定桶开始的迭代器 (public member function) | |
返回指向指定桶末尾的迭代器 (public member function) | |
返回桶的数量 (public member function) | |
返回最大桶数量 (public member function) | |
返回特定桶中的元素数量 (public member function) | |
返回特定键的桶 (public member function) | |
哈希策略 | |
返回每个桶的平均元素数量 (public member function) | |
管理每个桶的最大平均元素数量 (public member function) | |
预留至少指定数量的桶并重新生成哈希表 (public member function) | |
为至少指定数量的元素预留空间并重新生成哈希表 (public member function) | |
观察器 | |
返回用于哈希键的函数 (public member function) | |
返回用于比较键是否相等的函数 (public member function) |
[编辑] 非成员函数
(C++11)(C++11)(removed in C++20) |
比较 unordered_map 中的值 (function template) |
特化 std::swap 算法 (function template) | |
(C++20) |
擦除所有满足特定条件的元素 (function template) |
推导指引 |
(since C++17) |
[编辑] 注释
Feature-test 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的范围构造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::unordered_map |
[编辑] 示例
#include <iostream> #include <string> #include <unordered_map> int main() { // Create an unordered_map of three strings (that map to strings) std::unordered_map<std::string, std::string> u = { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} }; // Helper lambda function to print key-value pairs auto print_key_value = [](const auto& key, const auto& value) { std::cout << "Key:[" << key << "] Value:[" << value << "]\n"; }; std::cout << "Iterate and print key-value pairs of unordered_map, being\n" "explicit with their types:\n"; for (const std::pair<const std::string, std::string>& n : u) print_key_value(n.first, n.second); std::cout << "\nIterate and print key-value pairs using C++17 structured binding:\n"; for (const auto& [key, value] : u) print_key_value(key, value); // Add two new entries to the unordered_map u["BLACK"] = "#000000"; u["WHITE"] = "#FFFFFF"; std::cout << "\nOutput values by key:\n" "The HEX of color RED is:[" << u["RED"] << "]\n" "The HEX of color BLACK is:[" << u["BLACK"] << "]\n\n"; std::cout << "Use operator[] with non-existent key to insert a new key-value pair:\n"; print_key_value("new_key", u["new_key"]); std::cout << "\nIterate and print key-value pairs, using `auto`;\n" "new_key is now one of the keys in the map:\n"; for (const auto& n : u) print_key_value(n.first, n.second); }
可能的输出
Iterate and print key-value pairs of unordered_map, being explicit with their types: Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000] Iterate and print key-value pairs using C++17 structured binding: Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000] Output values by key: The HEX of color RED is:[#FF0000] The HEX of color BLACK is:[#000000] Use operator[] with non-existent key to insert a new key-value pair: Key:[new_key] Value:[] Iterate and print key-value pairs, using `auto`; new_key is now one of the keys in the map: Key:[new_key] Value:[] Key:[WHITE] Value:[#FFFFFF] Key:[BLACK] Value:[#000000] Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000]
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2050 | C++11 | reference 、 const_reference 、 pointer 的定义和 const_pointer 基于 allocator_type |
基于 value_type 和std::allocator_traits |
[编辑] 参见
(C++11) |
键值对的集合,通过键哈希 (class template) |
键值对的集合,按键排序,键是唯一的 (class template) | |
(C++23) |
适配两个容器以提供键值对的集合,按唯一键排序 (类模板) |