简体   繁体   English

从3D地图插入和读取值

[英]Inserting and Reading Values from 3D Map

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, 我有一张3D地图,

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. 现在我这样插入值,int的最大值为5但是对于每个int值,都有50 key and value子图的50 key and value

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. Temp1和TempB值超过50个值后,计数值将递增。 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? 现在,我想读回它的值,为此,我正在做这个问题中所述的方法如何循环遍历C ++地图? .

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. 由于int只有5,因此它仅显示TempA和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 谢谢

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. 如果需要包含所有值的子图,则需要先填充子图,然后再插入map3D或使用指向子图的指针。

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. 否则,每次要插入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));

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

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