2013-03-07 158 views
-1

如何打印多图的矢量? 例如我有看起来像这样的载体:打印地图矢量

typedef std::multimap<double,std::string> resultMap; 
typedef std::vector<resultMap> vector_results; 

EDIT

for(vector_results::iterator i = vector_maps.begin(); i!= vector_maps.end(); i++) 
{ 
    for(vector_results::iterator j = i->first.begin(); j!= i->second.end(); j++) 
    { 
     std::cout << j->first << " " << j->second <<std::endl; 
    } 
} 
+1

至少给它一个去,所以我们有东西上工作,你不能解决你的特殊问题。 – 2013-03-07 00:26:33

+0

@EdHeal:我其实是。 (vector_results :: iterator i = vector_maps.begin(); i!= vector_maps.end(); i ++){vector_results :: iterator j = i-> first.begin() ); j!= i-> second.end(); j ++){ \t std :: cout << j-> first << " " << j-> second << std :: endl; \t } \t \t } [漂亮地打印C++ STL容器]的' – 2013-03-07 00:27:12

+1

可能重复(http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) – ildjarn 2013-03-07 00:27:24

回答

1

在外部for循环点的i变量为resultMap,这意味着j变量的类型需要是resultMap::iterator。重写你的循环为

for(vector_results::const_iterator i = vector_maps.begin(); i != vector_maps.end(); ++i) 
{ 
    for(resultMap::const_iterator j = i->begin(); j != i->end(); ++j) 
    { 
     std::cout << j->first << " " << j->second << std::endl; 
    } 
} 

我改变了迭代器类型const_iterator因为迭代器不修改容器中的元素。


如果你的编译器支持C++ 11的范围内基于for循环,代码可以更写简洁

for(auto const& i : vector_maps) { 
    for(auto const& j : i) { 
    std::cout << j.first << " " << j.second << std::endl; 
    } 
}