std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[]

来自cppreference.com
 
 
 
 
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 起)

返回到映射到分别等于 keyx 的键的值的引用,这种键不存在的情况下就会进行插入。

1) 在键不存在的情况下插入从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 原位构造的 value_type 对象。
等价于 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) 在键不存在的情况下插入从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 原位构造的 value_type 对象。
等价于 return this->try_emplace(std::move(key)).first->second;(C++17 起)
使用默认分配器时,这导致从 key 移动构造键,并值初始化被映射值。
-
value_type 必须从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>()可就位构造(EmplaceConstructible) 。使用默认分配器时,这表明 key_type 必须为可移动构造(MoveConstructible) mapped_type 必须为可默认构造(DefaultConstructible)
3) 如果没有与 x 的值进行透明比较相等的键,则插入一个就地构造的 value_type 对象。
等价于 return this->try_emplace(std::forward<K>(x)).first->second;。 此重载仅在 此重载只有在Hash::is_transparentKeyEqual::is_transparent 均合法并指代类型时才会参与重载决议。这假设使得 Hash 能用 KKey 类型调用,并且 KeyEqual 是透明的,进而允许调用此函数时不需要构造 Key 的实例。时参与重载决议。它允许调用该函数时无需构建 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 问题 2469 所修复,而上面的描述合并了该问题的解决方案。

然而,已知一个实现(libc++)通过两个分离的分配器 construct() 调用来构造 key_typemapped_type 对象,可认为如发布时的标准所要求,而非原位构造 value_type 对象。

operator[] 非 const,因为它会在键不存在时插入键。如果此行为非所欲或容器为 const,那么可以使用 at

insert_or_assign 返回的信息多于 operator[],而且不要求 mapped_type 可默认构造。

(C++17 起)
功能特性测试标准功能特性
__cpp_lib_associative_heterogeneous_insertion202311L(C++26)剩余的有序无序关联容器的成员函数的异质重载。(3)

[编辑]示例

#include <iostream>#include <string>#include <unordered_map>   void println(autoconst comment, autoconst& map){std::cout<< comment <<"{";for(constauto&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 起初包含:", letter_counts);   letter_counts['b']=42;// 更新既存值 letter_counts['x']=9;// 插入新值   println("修改后它包含:", letter_counts);   // 统计每个单词的出现次数// (首次调用 operator[] 会初始化计数为零)std::unordered_map<std::string, int> word_map;for(constauto& w :{"this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax"})++word_map[w]; word_map["that"];// 插入对 {"that", 0}   for(constauto&[word, count]: word_map)std::cout<<"单词 '"<< word <<"' 出现 "<< count <<"次\n";}

可能的输出:

letter_counts 初始状态下包含:{{a: 27}{b: 3}{c: 1}} 修改后它包含:{{a: 27}{b: 42}{c: 1}{x: 9}} 单词 'a' 出现 2 次 单词 'hoax' 出现 1 次 单词 'is' 出现 2 次 单词 'not' 出现 1 次 单词 'sentence' 出现 3 次 单词 'that' 出现 0 次 单词 'this' 出现 2 次

[编辑]参阅

带越界检查访问指定的元素
(公开成员函数)[编辑]
插入元素,或若键已存在则赋值给当前元素
(公开成员函数)[编辑]
若键不存在则原位插入,若键存在则不做任何事
(公开成员函数)[编辑]
close