Inserting and Reading Values from 3D Map

谁都会走 提交于 2019-12-24 13:10:18

问题


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


回答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));


来源:https://stackoverflow.com/questions/24366401/inserting-and-reading-values-from-3d-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!