为了调试,我需要转储嵌套 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/