如果键不是映射中的初始化键,则STL映射[键]返回什么? [英] What does the STL map[key] return if the key wasn't a initialized key in the map?

查看:141
本文介绍了如果键不是映射中的初始化键,则STL映射[键]返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些示例代码:

 #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;
 }

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

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

推荐答案

默认构造的 std :: string code> code> std :: map ,并返回'b'的引用。

A default constructed std::string ins inserted into the std::map with key 'b' and a reference to that is returned.

请参阅文档,它定义了 operator [] as:

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


返回与特定键。如果映射没有包含这样的对象, operator [] 插入默认对象 data_type() p>

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().

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

(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.)

这篇关于如果键不是映射中的初始化键,则STL映射[键]返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆