std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[]
来自 cppreference.cn
T& operator[]( const Key& key ); |
(1) | (C++11 起) |
T& operator[]( Key&& key ); |
(2) | (C++11 起) |
template< class K > T& operator[]( K&& x ); |
(3) | (C++26 起) |
返回一个对与 `key` 或 `x` 等价的键所映射的值的引用,如果该键不存在,则执行插入操作。
1) 如果键不存在,则原地构造一个
value_type
对象,使用 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 构造。 等价于 return this->try_emplace(key).first->second;。(C++17 起)当使用默认分配器时,这将导致键从 `key` 复制构造,并且映射的值被 值初始化。
-value_type 必须可从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 就位构造(EmplaceConstructible)。当使用默认分配器时,这意味着 key_type 必须是 可复制构造(CopyConstructible) 的,并且 mapped_type 必须是 可默认构造(DefaultConstructible) 的。 |
2) 如果键不存在,则原地构造一个
value_type
对象,使用 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 构造。 等价于 return this->try_emplace(std::move(key)).first->second;。(C++17 起)
当使用默认分配器时,这将导致键从 `key` 移动构造,并且映射的值被 值初始化。
当使用默认分配器时,这将导致键从 `key` 移动构造,并且映射的值被 值初始化。
-value_type 必须可从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 就位构造(EmplaceConstructible)。当使用默认分配器时,这意味着 key_type 必须是 可移动构造(MoveConstructible) 的,并且 mapped_type 必须是 可默认构造(DefaultConstructible) 的。 |
等价于 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` 必须是 可默认插入(DefaultInsertable) 的,并且 key_type
必须是 可复制插入(CopyInsertable) 或 可移动插入(MoveInsertable) 到 *this 中的。此规范存在缺陷,已通过 LWG issue 2469 修复,上述描述已包含该问题的解决方案。
然而,已知一个实现(libc++)通过两次独立的分配器 `construct()` 调用来构造 key_type
和 `mapped_type` 对象,这可以说符合已发布的标准要求,而不是就地构造 value_type
对象。
operator[] 是非 const 的,因为它会在键不存在时插入键。如果不需要此行为,或者容器是 const 的,则可以使用 at
。
|
(C++17 起) |
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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) |