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

std::map::operator[]

T& operator;

(1)

T& operator;

(2)

(since C++11)

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

1) Inserts value_type(key, T()) if the key does not exist. This function is equivalent to return insert(std::make_pair(key, T())).first->second; -key_type must meet the requirements of CopyConstructible. -mapped_type must meet the requirements of CopyConstructible and DefaultConstructible. If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned.

-key_type must meet the requirements of CopyConstructible.

-mapped_type must meet the requirements of CopyConstructible and DefaultConstructible.

(until C++11)

-钥匙[医]类型必须符合CopyConstrucable的要求。

-映射[医]类型必须符合CopyConstrucable和DefaultConstrucable的要求。

1%29插入一个值[医]从std::分段构造就地构造的对象类型[医]结构,STD::前进[医]如[医]tuple%28 key%29,std::tuple<>%28%29(如果不存在键)。此函数等效于返回此->TRY[医]嵌入%28键%29,第一->第二;%28由于使用默认分配程序时C++17%29,这将导致从键构造密钥并将映射值初始化。-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28键%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是CopyConstrucable并映射[医]类型必须是DefaultConstrucable。2%29插入一个值[医]从std::分段构造就地构造的对象类型[医]结构,STD::前进[医]如[医]元组%28 std::如果不存在键,则移动%28 key%29%29,std::tuple<>%28%29。此函数等效于返回此->TRY[医]位置%28 std::移动%28键%29%29第一->秒。%28由于使用默认分配程序时C++17%29,这将导致键从键中移动,映射的值被初始化。-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28 std::移动%28 key%29%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是可移动的并映射[医]类型必须是DefaultConstrucable。-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28键%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是CopyConstrucable并映射[医]类型必须是DefaultConstrucable。-价值[医]类型必须是EmplaceConstrucable从STD::分段[医]结构,STD::前进[医]如[医]元组%28 std::移动%28 key%29%29,std::tuple<>%28%29。当使用默认分配器时,这意味着[医]类型必须是可移动的并映射[医]类型必须是DefaultConstrucable。%28自C++11%29

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

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

没有迭代器或引用无效。

参数

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 <map>
 
int main()
{
    std::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::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 (C++11)

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。

扫码关注腾讯云开发者

领取腾讯云代金券