首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unordered_map::operator[]

T& operator;

(1)

(since C++11)

T& operator;

(2)

(since C++11)

返回映射到对应于key,如果该键不存在,则执行插入。

1%29插入avalue_type对象的std::piecewise_construct,std::forward_as_tuple(key),std::tuple<>()如果密钥不存在。此函数等价于return this->try_emplace(key).first->second;.%28自C++17%29

当使用默认分配器时,这将导致从key映射值为值初始化...

-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28键%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是CopyConstrucable并映射[医]类型必须是DefaultConstrucable。

*。

2%29插入avalue_type对象的std::piecewise_construct,std::forward_as_tuple(std::move(key)),std::tuple<>()如果密钥不存在。此函数等价于return this->try_emplace(std::move(key)).first->second;.%28自C++17%29

当使用默认分配器时,这将导致从key映射值为值初始化...

-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28 std::移动%28 key%29%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是可移动的并映射[医]类型必须是DefaultConstrucable。

*。

如果发生插入并导致重新散列容器,则所有迭代器都将失效。否则迭代器不会受到影响。引用不失效。只有当新元素数大于max_load_factor()*bucket_count()...

参数

key

-

the key of the element to find

返回值

如果没有带键的元素,则引用新元素的映射值key已经存在了。否则,引用其键等价于key...

例外

如果任何操作引发异常,则插入无效。

复杂性

平均情况:常量,最坏情况:线性大小。

注记

在已发布的C++11和C++14标准中,此函数被指定为mapped_type成为DefaultInsertablekey_type成为CopyInsertableMoveInsertable*this.本说明书有缺陷,由lwg第2469期,上面的描述包含了这个问题的解决。

但是,已知有一个实现%28 libc++%29可以构造key_typemapped_type对象通过两个单独的分配器。construct()调用,可以说是公布的标准所要求的,而不是放置一个value_type对象。

operator[]是非连接的,因为如果它不存在%27T,它就插入键。如果这种行为是不可取的,或者如果容器是const,,,at()可能会被使用。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
 
int main()
{
    std::unordered_map<char, int> letter_counts {{'a', 27}, {'b', 3}, {'c', 1}};
 
    std::cout << "initially:\n";
    for (const auto &pair : letter_counts) {
        std::cout << pair.first << ": " << pair.second << '\n';
    }
 
    letter_counts['b'] = 42;  // update an existing value
    letter_counts['x'] = 9;  // insert a new value
 
    std::cout << "after modifications:\n";
    for (const auto &pair : letter_counts) {
        std::cout << pair.first << ": " << pair.second << '\n';
    }
 
    // count the number of occurrences of each word
    // (the first call to operator[] initialized the counter with zero)
    std::unordered_map<std::string, size_t>  word_map;
    for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
                           "this", "sentence", "is", "a", "hoax"}) {
        ++word_map[w];
    }
 
    for (const auto &pair : word_map) {
        std::cout << pair.second << " occurrences of word '" << pair.first << "'\n";
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
initially:
a: 27
b: 3
c: 1
after modifications:
a: 27
b: 42
c: 1
x: 9
2 occurrences of word 'a'
1 occurrences of word 'hoax'
2 occurrences of word 'is'
1 occurrences of word 'not'
3 occurrences of word 'sentence'
2 occurrences of word 'this'

二次

另见

at

access specified element with bounds checking (public member function)

insert_or_assign (C++17)

inserts an element or assigns to the current element if the key already exists (public member function)

try_emplace (C++17)

inserts in-place if the key does not exist, does nothing if the key exists (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com