std::map<Key,T,Compare,Allocator>:: operator[]
|
T
&
operator
[
]
(
const
Key
&
key
)
;
|
(1) | |
|
T
&
operator
[
]
(
Key
&&
key
)
;
|
(2) | (自 C++11 起) |
|
template
<
class
K
>
T & operator [ ] ( K && x ) ; |
(3) | (自 C++26 起) |
返回对映射到与 key 或 x 等效的键所对应的值的引用,如果该键尚不存在,则执行插入操作。
|
1)
若键不存在,则插入
value_type
(
key, T
(
)
)
。
|
(C++11 前) | ||||||
|
1)
若键不存在,则就地构造并插入
value_type
对象,构造参数为
std::
piecewise_construct
,
std::
forward_as_tuple
(
key
)
,
std::
tuple
<>
(
)
。
等价于
return
this
-
>
try_emplace
(
key
)
.
first
-
>
second
;
。
(C++17 起)
使用默认分配器时,这将导致键从
key
拷贝构造,映射值进行
值初始化
。
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
移动构造,映射值进行
值初始化
。
|
(C++11 起) | ||||||
Compare
为
透明函数对象
时参与重载决议。它允许在不构造
Key
实例的情况下调用此函数。
不会使任何迭代器或引用失效。
目录 |
参数
| key | - | 要查找元素的键 |
| x | - | 可与键进行透明比较的任意类型值 |
返回值
异常
如果任何操作抛出异常,则插入操作不会产生任何效果。
复杂度
与容器大小呈对数关系。
注释
在已发布的C++11和C++14标准中,此函数曾被要求
mapped_type
需满足
可默认插入
,且
key_type
需满足
可拷贝插入
或
可移动插入
至
*
this
。该规范存在缺陷,后由
LWG问题2469
修复,上文描述已包含该问题的解决方案。
然而,已知有一种实现(libc++)会通过两次独立的分配器
construct()
调用来构造
key_type
和
mapped_type
对象——这 arguably 符合已发布标准的要求——而非直接置入
value_type
对象。
operator
[
]
是非常量成员函数,因为它会在键不存在时执行插入操作。若不需要此行为或容器为
const
状态,可使用
at
方法。
|
|
(since C++17) |
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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; // 更新现有值 letter_counts['x'] = 9; // 插入新值 println("after modifications it contains: ", letter_counts); // 统计每个单词的出现次数 // (首次调用 operator[] 会将计数器初始化为零) 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"]; // 仅插入键值对 {"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++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 334 | C++98 |
重载
(1)
的效果仅为返回
( * ( ( insert ( std:: make_pair ( x, T ( ) ) ) ) . first ) ) . second |
改为提供其自身的
描述 |
参见
|
访问指定元素并进行边界检查
(公开成员函数) |
|
|
(C++17)
|
插入元素,若键已存在则赋值给当前元素
(公开成员函数) |
|
(C++17)
|
若键不存在则就地插入,若键存在则不执行任何操作
(公开成员函数) |