c++ - 如何在 C++ 中转储嵌套映射以进行调试?

标签 c++ iterator

为了调试,我需要转储嵌套 map 的内容。我试图用以下代码对此进行描述:

struct Foo
{       
   string name;
};

typedef std::map<string, Foo> MapFoo;

struct Bar  {
  string name;    
  MapFoo mapFoo;
};

typedef std::map<string, Bar> MapBar;

...

MapBar mapBar = init_mapBar(); 

const MapBar::const_iterator it = mapBar.find("name");

if ( mapBar.end() != it )
{
    return it->second;
}

在我返回它之前->第二,我想将该项目的内容转储到cout。尝试这样做时,我迷失了迭代器。非常感谢您的帮助或提示。

最佳答案

你可以分开使用两个。一个 map <string, int>和一个 map<string, map<string, int> > .

例如,像这样的东西:
注意:我还没有测试代码。

map <string, map <string, int> > foo;
// fill map
map <string, map <string, int> >::iterator outerit;

map <string, int>::iterator innerit;

for (outerit = foo.begin(); outerit != foo.end(); ++outerit)
{
    for (innerit = outerit->second.begin(); innerit != outerit->second.end();++innerit)
    {
        cout << outerit->first << " " <<  innerit->first << " " << innerit->second << "\n";
        //Write contents to a file here
    }
}

关于c++ - 如何在 C++ 中转储嵌套映射以进行调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6343285/

相关文章:

c++ - 项目编译时不能使用 gvim

c# - 特殊类型查询的数据结构

C++ std::vector 迭代器行为奇怪,不允许递增

Java - 按降序遍历 Mapset,返回所需的输出

c++ - 记录第二个键盘事件

c++ - 将非静态方法传递给泛型函数

c++ - setupUi() 背后的设计原因

C++;如何比较遍历相同字符串 vector 的不同迭代器的字符串值

c++ - 如何删除和删除指向 vector 中存储的对象的指针?

java - 调用所有集合成员的删除方法