简体   繁体   English

在std :: map中插入std :: map

[英]insert std::map in std::map

I have a container of std::map<string,std::map<string,int>> . 我有一个std::map<string,std::map<string,int>>的容器。

  1. How do I insert data into such a container? 如何将数据插入此类容器? Do I have to have an inner map as additional variable or not? 我是否必须将内部地图作为附加变量? The code should compile under both MSVC 2010 and XCode 4.2 (Snow Leopard). 代码应该在MSVC 2010和XCode 4.2(Snow Leopard)下编译。

  2. Is XCode 4.2 under Snow Leopard (10.6) supports such a container? Snow Leopard(10.6)下的XCode 4.2是否支持这样的容器?

Just use the overloaded [] operator and you are done: 只需使用重载[]运算符即可完成:

std::map<string,std::map<string,int> > data;
data["foo"]["bar"] = 10;

and yes, Xcode 4.2 supports them, I personally used them under OSX with no problems. 是的,Xcode 4.2支持它们,我个人在OSX下使用它们没有问题。

As a simple answer you need to have a temporary map: 作为一个简单的答案,您需要有一个临时地图:

std::map<string, int> tempMap;
std::string tempString;

Then you will need to insert these into the above map once they have been set. 然后,您需要在设置后将这些插入到上面的地图中。

tempMap.insert(std::pair<string, std::map<string, int>>(tempString, tempMap))

Then you can access them as in Jacks answer. 然后你可以像Jacks的回答一样访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM