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

pos*_*eid 0 c++ iterator

为了调试我需要转储嵌套映射的内容.我试着在下面的代码中描述这个:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Alo*_*ave 5

你可以使用两个单独的.一个用于a map <string, int>,一个用于a 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
    }
}
Run Code Online (Sandbox Code Playgroud)