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

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

问题描述

以下是一些示例代码:

 #include< iostream> 
#include< map>
#include< string>
使用命名空间std;

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

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

解决方案

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



查阅文档通常很有用,定义 operator [] 的行为:


返回对对象的引用这与特定密钥相关联。如果地图还没有包含这样的对象, operator [] 插入默认对象 data_type()。 (SGI STL文档不是C ++标准库的文档,但它仍然是无价的资源,因为标准的大部分行为图书馆容器与SGI STL容器的行为相同或非常接近。)


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?

解决方案

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

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

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

(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地图[key]返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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