std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[]
来自 cppreference.cn
< cpp | container | unordered map
T& operator[]( const Key& key ); |
(1) | (since C++11) |
T& operator[]( Key&& key ); |
(2) | (since C++11) |
template< class K > T& operator[]( K&& x ); |
(3) | (since C++26) |
返回到映射到等价于 key 或 x 的键的值的引用,如果该键尚不存在,则执行插入。
1) 如果键不存在,则就地从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 构造
value_type
对象并插入。 等价于 return this->try_emplace(key).first->second;。(since C++17)当使用默认分配器时,这将导致键从
key
复制构造,并且映射的值被值初始化。 -value_type 必须是从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 就地构造的。当使用默认分配器时,这意味着 key_type 必须是 可复制构造 的,并且 mapped_type 必须是 可默认构造 的。 |
2) 如果键不存在,则就地从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 构造
value_type
对象并插入。 等价于 return this->try_emplace(std::move(key)).first->second;。(since C++17)
当使用默认分配器时,这将导致键从
当使用默认分配器时,这将导致键从
key
移动构造,并且映射的值被值初始化。 -value_type 必须是从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 就地构造的。当使用默认分配器时,这意味着 key_type 必须是 可移动构造 的,并且 mapped_type 必须是 可默认构造 的。 |
等价于 return this->try_emplace(std::forward<K>(x)).first->second;。仅当 Hash::is_transparent 和 KeyEqual::is_transparent 有效且各自表示一种类型时,此重载才参与重载解析。这假定这样的
Hash
可以使用 K
和 Key
类型调用,并且 KeyEqual
是透明的,这两者共同允许在不构造 Key
实例的情况下调用此函数。如果在操作之后,新元素数量大于旧的 max_load_factor()
*
bucket_count()
,则会发生重哈希。
如果发生重哈希(由于插入),则所有迭代器都将失效。否则(不重哈希),迭代器不会失效。
内容 |
[编辑] 参数
key | - | 要查找的元素的键 |
x | - | 可以与键透明比较的任何类型的值 |
[编辑] 返回值
1,2) 如果不存在键为 key 的元素,则返回对新元素的映射值的引用。否则,返回对键等价于 key 的现有元素的映射值的引用。
3) 如果不存在键与值 x 比较等价的元素,则返回对新元素的映射值的引用。否则,返回对键与 x 比较等价的现有元素的映射值的引用。
[编辑] 异常
如果任何操作抛出异常,则插入无效。
[编辑] 复杂度
平均情况:常数时间,最坏情况:与大小呈线性关系。
[编辑] 注意
在已发布的 C++11 和 C++14 标准中,此函数被指定为要求 mapped_type
是 可默认插入 的,并且 key_type
是 可复制插入 或 可移动插入 到 *this 中。此规范存在缺陷,并已通过 LWG issue 2469 修复,并且以上描述包含了该问题的解决方案。
但是,已知一种实现 (libc++) 通过两个单独的分配器 construct()
调用来构造 key_type
和 mapped_type
对象,正如已发布的标准可能要求的那样,而不是就地构造 value_type
对象。
operator[] 是非 const 的,因为它在键不存在时插入键。如果不需要此行为,或者如果容器是 const,则可以使用 at
。
|
(since C++17) |
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_associative_heterogeneous_insertion |
202311L |
(C++26) | 在 有序和无序关联容器中,为剩余的成员函数提供异构重载。 (3) |
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <unordered_map> void println(auto const comment, auto const& map) { std::cout << comment << '{'; for (const auto& pair : map) std::cout << '{' << pair.first << ": " << pair.second << '}'; std::cout << "}\n"; } int main() { std::unordered_map<char, int> letter_counts{{'a', 27}, {'b', 3}, {'c', 1}}; println("letter_counts initially contains: ", letter_counts); letter_counts['b'] = 42; // updates an existing value letter_counts['x'] = 9; // inserts a new value println("after modifications it contains: ", letter_counts); // count the number of occurrences of each word // (the first call to operator[] initialized the counter with zero) std::unordered_map<std::string, int> word_map; for (const auto& w : {"this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax"}) ++word_map[w]; word_map["that"]; // just inserts the pair {"that", 0} for (const auto& [word, count] : word_map) std::cout << count << " occurrence(s) of word '" << word << "'\n"; }
可能的输出
letter_counts initially contains: {{a: 27}{b: 3}{c: 1}} after modifications it contains: {{a: 27}{b: 42}{c: 1}{x: 9}} 2 occurrence(s) of word 'a' 1 occurrence(s) of word 'hoax' 2 occurrence(s) of word 'is' 1 occurrence(s) of word 'not' 3 occurrence(s) of word 'sentence' 0 occurrence(s) of word 'that' 2 occurrence(s) of word 'this'
[编辑] 参见
访问带有边界检查的指定元素 (public member function) | |
(C++17) |
插入元素,如果键已存在,则赋值给当前元素 (public member function) |
(C++17) |
如果键不存在则就地插入,如果键存在则不执行任何操作 (public member function) |