1

First of all I read all the previously asked questions related to this questions, but didn't able to solve my problem. I have one 3D Map,

map<int,map<const char*, const char*>> map3D;
map<const char*, const char*> submap;

Now i inserted valued in it like this, the max of value of int is 5 but for each int value there is 50 key and value for submap.

I inserted the value like this

submap.insert(std::pair<const char*,const char*>(TempA, TempB));
map3D.insert(pair<int,map<const char*,const char*>>(count,submap));

count value will be incremented after Temp1 and TempB values will be more than 50 values. Now i want to read it back the values and for this i am doing like this as mentioned in this question How to loop through a C++ map of maps?.

std::map<int,map<const char*,const char*>>::iterator out_it;
    for(out_it =map3D.begin();out_it != map3D.end();++out_it)
    {
        for(std::map<const char*, const char*>::const_iterator in_it=out_it->second.begin();in_it != out_it->second.end(); ++in_it)
            std::cout << in_it->first << " => " << in_it->second <<endl;
}

As int is only 5 so it is showing only five values of TempA and TempB. If iterate only in submap, it is showing all the values

for(std::map<const char*, const char*>::const_iterator in_it=submap.begin();in_it != submap.end(); ++in_it)
                std::cout << in_it->first << " => " << in_it->second <<endl;

Can Anyone guide me how i can read all the values. Thanks

Community
  • 1
  • 1
Hamid
  • 137
  • 1
  • 11

1 Answers1

1

Everything is correct. Because you code every time inserts the same submap:

submap.insert(std::pair<const char*,const char*>(TempA, TempB));
map3D.insert(pair<int,map<const char*,const char*>>(count,submap));

If you need submap that contains all values, so you need fill your submap first and than insert into the map3D or use pointer to submap.

map<int,map<const char*, const char*>*> map3D;
map<const char*, const char*>* submap = new map<const char*, const char*>();
...
submap->insert(std::pair<const char*,const char*>(TempA, TempB));
map3D.insert(pair<int,map<const char*,const char*>*>(count,submap));

Otherwise your need to create submap each time when you want to insert into the map3D.

map<const char*, const char*> submap;
submap.insert(std::pair<const char*,const char*>(TempA, TempB));
map3D.insert(pair<int,map<const char*,const char*>>(count,submap));
Denis Zaikin
  • 569
  • 4
  • 10