C++ 如果键不是映射中的初始化键,STL map[key] 会返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5087091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:20:35  来源:igfitidea点击:

What does the STL map[key] return if the key wasn't a initialized key in the map?

c++stlmap

提问by Mirza

Here is some example code:

下面是一些示例代码:

 #include<iostream>
 #include<map>
 #include<string>
 using namespace std;

 int main()
 {
   map<char, string> myMap;
   myMap['a'] = "ahh!!";
   cout << myMap['a'] << endl << myMap['b'] << endl;
   return 0;
 }

In this case i am wondering what does myMap['b'] return?

在这种情况下,我想知道 myMap['b'] 返回什么?

回答by James McNellis

A default constructed std::stringins inserted into the std::mapwith key 'b'and a reference to that is returned.

std::string插入到std::mapwith 键中的默认构造ins'b'并返回对它的引用。

It is often useful to consult the documentation, which defines the behavior of operator[]as:

查阅文档通常很有用,该文档将以下行为定义operator[]为:

Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[]inserts the default object data_type().

返回对与特定键关联的对象的引用。如果地图尚未包含此类对象,则operator[]插入默认对象data_type()

(The SGI STL documentation is not documentation for the C++ Standard Library, but it is still an invaluable resource as most of the behavior of the Standard Library containers is the same or very close to the behavior of the SGI STL containers.)

(SGI STL 文档不是 C++ 标准库的文档,但它仍然是无价的资源,因为标准库容器的大多数行为与 SGI STL 容器的行为相同或非常接近。)

回答by Hyman Saalw?chter

A default-constructed object (eg, an empty string in this case) is returned.

返回默认构造的对象(例如,在这种情况下为空字符串)。

This is actually returned even when you say map['a'] = "ahh!!";. The [] operator inserts a default-constructed string at position 'a', and returns a reference to it, which the = operator is then called on.

即使您说map['a'] = "ahh!!";. [] 运算符在位置 'a' 插入一个默认构造的字符串,并返回对其的引用,然后调用 = 运算符。

回答by Asha

std::mapoperator[]inserts the default constructed value type in to the map if the key provided for the lookup doesn't exist. So you will get an empty string as the result of the lookup.

std::mapoperator[]如果为查找提供的键不存在,则将默认构造的值类型插入到映射中。所以你会得到一个空字符串作为查找的结果。

回答by Mohammad Ali Akber

If you try to access a key value using indexing operator [], then 2 things can happen :

如果您尝试使用索引运算符访问键值[],则可能会发生两件事:

  1. The map contains this key. So it will return the corresponding key value
  2. The map doesn't contain the key. In this case it will automatically add a keyto the map with key value null.
  1. 地图包含此密钥。所以它会返回对应的键值
  2. 地图不包含密钥。在这种情况下,它将automatically add a key使用key value null.

As 'b'key is not in your map so it will add this key with value ""(empty string) automatically and it will print this empty string.

由于'b'键不在您的地图中,因此它会自动添加带有值""(空字符串)的键,并打印此空字符串。

And here map size will increase by 1

这里地图大小将增加 1

So to look-up for a key you can use .find(), which will return map.end()if the key is not found.
And no extra key will be added automatically

因此,要查找您可以使用的密钥.find()map.end()如果未找到该密钥,它将返回。
并且不会自动添加额外的密钥

And obviously you can use []operator when you set a value for a key

显然,您可以[]在为键设置值时使用运算符