std::map<Key,T,Compare,Allocator>::operator[]
来自 cppreference.cn
T& operator[]( const Key& key ); |
(1) | |
T& operator[]( Key&& key ); |
(2) | (since C++11) |
template< class K > T& operator[]( K&& x ); |
(3) | (since C++26) |
返回映射到等价于 key 或 x 的键的值的引用,如果该键尚不存在,则执行插入。
1) 如果键不存在,则插入 value_type(key, T())。
|
(until C++11) | ||||||
1) 如果键不存在,则插入一个从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 就地构造的
value_type 对象。 等效于 return this->try_emplace(key).first->second;。(since C++17)当使用默认分配器时,这会导致键从
key 复制构造,并且映射的值被值初始化。
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 移动构造,并且映射的值被值初始化。
|
(since C++11) |
等效于 return this->try_emplace(std::forward<K>(x)).first->second;。仅当限定标识符 Compare::is_transparent 有效并表示类型时,此重载才参与重载解析。它允许在不构造
Key
实例的情况下调用此函数。没有迭代器或引用失效。
目录 |
[编辑] 参数
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
。
|
(since C++17) |
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_associative_heterogeneous_insertion |
202311L |
(C++26) | 用于 有序和 无序 关联 容器中剩余成员函数的异构重载。 (3) |
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <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::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::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'
缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 334 | C++98 | 重载 (1) 的效果只是返回 (*((insert(std::make_pair(x, T()))).first)).second |
提供了自己的 描述代替 |
[编辑] 参见
访问带有边界检查的指定元素 (public member function) | |
(C++17) |
插入元素或赋值给当前元素(如果键已存在) (public member function) |
(C++17) |
如果键不存在则就地插入,如果键存在则不执行任何操作 (public member function) |