CppDS.com

C++ 98 11 14 17 20 手册

std::map<Key,T,Compare,Allocator>::operator[]

来自cppreference.com
< cpp‎ | container‎ | map

T& operator[]( const Key& key );
(1)
T& operator[]( Key&& key );
(2) (C++11 起)

返回到映射到等于 key 的键的值的引用,若这种键不存在则进行插入。

1) 若键不存在则插入 value_type(key, T()) 。此函数等价于 return insert(std::make_pair(key, T())).first->second;
-
key_type 必须满足可复制构造 (CopyConstructible) 的要求。
-
mapped_type 必须满足可复制构造 (CopyConstructible) 可默认构造 (DefaultConstructible) 的要求。
若进行插入,则值初始化被映射值(对类类型为默认构造,否则为零初始化)并返回到它的引用。
(C++11 前)
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)
(C++11 起)

没有迭代器或引用被非法化。

参数

key - 要寻找的元素键

返回值

若不存在拥有关键 key 的元素,则为到新元素被映射值的引用。否则为到既存的关键等价于 key 的元素的被映射值的引用。

异常

若任何操作抛出异常,则插入无效果。

复杂度

与容器大小成对数。

注解

出版的 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 起)

示例

#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;  // 更新既存值
    letter_counts['x'] = 9;  // 插入新值
 
    std::cout << "after modifications:\n";
    for (const auto &pair : letter_counts) {
        std::cout << pair.first << ": " << pair.second << '\n';
    }
 
    // 统计每个词的出现数
    // (首次调用 operator[] 以零初始化计数器)
    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";
    }
}

输出:

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'

参阅

(C++11)
访问指定的元素,同时进行越界检查
(公开成员函数)
插入元素,或若键已存在则赋值给当前元素
(公开成员函数)
若键不存在则原位插入,若键存在则不做任何事
(公开成员函数)
关闭