从3D地图插入和读取值 [英] Inserting and Reading Values from 3D Map

查看:174
本文介绍了从3D地图插入和读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我阅读了有关此问题的所有以前提出的问题,但无法解决我的问题。我有一个3D地图,

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;

现在我插入了这样的值,int的最大值为 5 但是对于每个int值,子图中有 50个键和值

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.

我插入了这样的值

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

Temp1和TempB值将超过50个值后,计数值将增加。现在我想读取这些值,为此我正在这样做,就像这个问题如何循环通过地图的C ++映射?

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;
}

由于int只有5,所以它只显示TempA和TempB的五个值。如果仅在子图中迭代,则显示所有值(

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

推荐答案

一切正确。因为每次代码插入相同的子地图:

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

如果您需要包含所有值的子图,则需要首先填充您的子图,而不是插入到map3D或使用指向子图的指针。

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

否则,您需要在每次要插入到map3D中时创建子图。

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

这篇关于从3D地图插入和读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆