Map nested in a map

I am trying to nest a map into a map. However I do not understand how can I insert something in it? This is how I am declaring the map

 
  std::map<int,std::map<std::string,double> > map_data;


How do I insert say 0,window,3.14 in it? Also is there anyway I can name the inner map and use it separately other than declaring 2 different maps in 2 different lines.
1
2
3
4
5
6
7
std::map< int,std::map<std::string,double> > map_data;

std::map<std::string, double> data;

data.emplace("window", 3.14);

map_data.emplace(0, data);
oh thanks.. one more question why emplace? why not insert or []??
You could also use this:

map_data[7]["hello"] = 3.1415926;

However in some cases it is better to emplace for efficiency. I would recommend only using emplace if you actually profile your code and find that the above method is really the slowest part of your program - otherwise you need not be concerned about efficiency.
This works great but when i try to print i get only one value :( this is how i am trying to print the contents

1
2
3
4
5
6
7
8
9
std::map<std::string, double>::iterator itr1;
std::map<int, std::map<std::string, double> >::iterator itr2;
for(itr2 = map_data.begin(); itr2 != map_data.end(); itr2++)
{
for(itr1 = data.begin(); itr1 != data.end(); itr1++)
{
std::cout << "\n*^*Window: " << itr2->first << " \ " << itr1->first << " \ " << itr1->second;
}
}
You can't use data anymore after you add it to map_data, since it won't update when its copy inside map_data does. You need to iterate over iter2->second.
I think there is a potential semantic error. There is no "a map" in a map. There are many maps in a map. Each (int) key on the outer map corresponds to different <string,double> map.

If the "1 in 1" is just a typo, then everything is ok, but if it does affect thinking too, ...
I figured out the problem.. i need to use the iterator of the map_data to access the inner map.. like itr2->second.first and it works.. thank u all for ur help :)
Topic archived. No new replies allowed.