Standard Template Library (STL) - std::map::operator[]

Standard Template Library (STL) - std::map::operator[]

public member function - 公开成员函数

1. std::map::operator[]

C++98
mapped_type& operator[] (const key_type& k);

C++11
mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);

Access element - 访问或插入指定的元素

If k matches the key of an element in the container, the function returns a reference to its mapped value.
如果 k 与容器中元素的键匹配,则该函数返回对其映射值的引用。

If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
如果 k 与容器中任何元素的键都不匹配,该函数将使用该键插入一个新元素,并返回对其映射值的引用。请注意,即使没有将映射值分配给元素 (该元素是使用其默认构造函数构造的),这也会始终使容器大小增加 1。

A similar member function, map::at, has the same behavior when an element with the key exists, but throws an exception when it does not.
当具有键的元素存在时,类似的成员函数 map::at 具有相同的行为,但不存在时会引发异常。

A call to this function is equivalent to: (对该函数的调用等效于:)
(*((this->insert(make_pair(k,mapped_type()))).first)).second

2. Parameters

k
Key value of the element whose mapped value is accessed.
访问其映射值的元素的键值。

Member type key_type is the type of the keys for the elements stored in the container, defined in map as an alias of its first template parameter (Key).
成员类型 key_type 是存储在容器中的元素的键的类型,在 map 中定义为其第一个模板参数 (Key) 的别名。

If an rvalue (second version), the key is moved instead of copied when a new element is inserted.
如果是右值 (第二版),则在插入新元素时将移动键,而不是复制键。

3. Return value

A reference to the mapped value of the element with a key value equivalent to k.
用等于 k 的键值引用元素的映射值。

Member type mapped_type is the type of the mapped values in the container, defined in map as an alias of its second template parameter (T).
成员类型 mapping_type 是容器中映射值的类型,在 map 中定义为其第二个模板参数 (T) 的别名。

4. Examples

4.1 std::map::operator[]

//============================================================================
// Name        : std::map::operator[]
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <map>
#include <string>

int main()
{
	std::map<char, std::string> map_data;

	map_data['a'] = "an element";
	map_data['b'] = "another element";
	map_data['c'] = map_data['b'];

	std::cout << "map_data['a'] is " << map_data['a'] << '\n';
	std::cout << "map_data['b'] is " << map_data['b'] << '\n';
	std::cout << "map_data['c'] is " << map_data['c'] << '\n';
	std::cout << "map_data['d'] is " << map_data['d'] << '\n';

	std::cout << "map_data now contains " << map_data.size() << " elements.\n";

	return 0;
}

Notice how the last access (to element 'd') inserts a new element in the map with that key and initialized to its default value (an empty string) even though it is accessed only to retrieve its value. Member function map::find does not produce this effect.
请注意,最后一次访问 (对元素 'd') 使用该键在映射中插入新元素,并初始化为其默认值 (空字符串),即使仅通过访问来检索其值也是如此。成员函数 map::find不会产生此效果。

map_data['a'] is an element
map_data['b'] is another element
map_data['c'] is another element
map_data['d'] is 
map_data now contains 4 elements.

5. Complexity - 复杂度

Logarithmic in size.
与容器大小成对数。

6. Iterator validity - 迭代器有效性

No changes.

7. Data races - 数据竞争

The container is accessed, and potentially modified.
容器已被访问,并可能被修改。

The function accesses an element and returns a reference that can be used to modify its mapped value. Concurrently accessing other elements is safe.
该函数访问一个元素并返回一个可用于修改其映射值的引用。同时访问其他元素是安全的。

If the function inserts a new element, concurrently iterating ranges in the container is not safe.
如果函数插入新元素,则在容器中同时迭代范围是不安全的。

8. Exception safety - 异常安全性

Strong guarantee: if an exception is thrown, there are no changes in the container.
有力的保证:如果引发异常,则容器中没有任何更改。

If a new element is inserted and allocator_traits::construct cannot construct an element with k and a default-constructed mapped_type (or if mapped_type is not default constructible), it causes undefined behavior.
如果插入了新元素,并且 allocator_traits::construct 无法构造具有 k 且具有默认构造的 mapping_type 的元素 (或者如果 mapped_type 不是默认可构造的),则它将导致未定义的行为。

References

http://www.cplusplus.com/reference/map/map/operator[]/

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104217797