简体   繁体   中英

insert std::map in std::map

I have a container of 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).

  2. Is XCode 4.2 under Snow Leopard (10.6) supports such a container?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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