2013-03-07 87 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; 
    } 
}