2009-06-30 78 views

回答

22

那麼這取決於你想如何顯示它們,但你總是可以輕鬆地重複他們:

typedef map<string, list<string>>::const_iterator MapIterator; 
for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) 
{ 
    cout << "Key: " << iter->first << endl << "Values:" << endl; 
    typedef list<string>::const_iterator ListIterator; 
    for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++) 
     cout << " " << *list_iter << endl; 
} 
10

我想嘗試以下

void dump_list(const std::list<string>& l) { 
    for (std::list<string>::const_iterator it = l.begin(); l != l.end(); l++) { 
    cout << *l << endl; 
    } 
} 

void dump_map(const std::map<string, std::list<string>>& map) { 
    for (std::map<string,std::list<string>>::const_iterator it = map.begin(); it != map.end(); it++) { 
    cout << "Key: " << it->first << endl; 
    cout << "Values" << endl; 
    dump_list(it->second); 
} 
+0

在第一行得到預期的非限定id之前<'token':void dump_list(const std :: list &l){`。我必須包括一些東西嗎? – 2013-01-15 14:33:38

+0

在dump_list中應該是這樣的: std :: vector :: const_iterator it = l.begin(); (It; it!= l.end(); ++ it){ std :: cout << * it << std :: endl; } 而不是在cout!否則這對我很有幫助 /謝謝! – Kahin 2015-01-29 14:52:50

4

我有點偏離主題這裏...

我想你想轉儲地圖內容進行調試。我想提一提的是,下一個gdb版本(版本7.0)將包含一個內置的python解釋器,gcc libstdC++將使用它來提供漂亮的打印機。這裏是你的情況

#include <map> 
    #include <map> 
    #include <list> 
    #include <string> 

    using namespace std; 

    int main() 
    { 
    typedef map<string, list<string> > map_type; 
    map_type mymap; 

    list<string> mylist; 
    mylist.push_back("item 1"); 
    mylist.push_back("item 2"); 
    mymap["foo"] = mylist; 
    mymap["bar"] = mylist; 

    return 0; // stopped here 
    } 

導致

(gdb) print mymap 
$1 = std::map with 2 elements = { 
    ["bar"] = std::list = { 
    [0] = "item 1", 
    [1] = "item 2" 
    }, 
    ["foo"] = std::list = { 
    [0] = "item 1", 
    [1] = "item 2" 
    } 
} 

耶的例子!

2

另一種形式,採用<algorithm>

void printPair(const pair<string, list<string> > &p) 
{ 
    cout << "Key: " << p.first << endl; 
    copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n")); 
}  
for_each(mapex.begin(), mapex.end(), printPair); 

測試程序:

#include <iostream> 
#include <map> 
#include <list> 
#include <iterator> 
#include <algorithm> 
using namespace std; 

void printPair(const pair<string, list<string> > &p) 
{ 
    cout << "Key: " << p.first << endl; 
    copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n")); 
} 

int main() 
{ 
    map<string, list<string> > mapex; 

    list<string> mylist1; 
    mylist1.push_back("item 1"); 
    mylist1.push_back("item 2"); 
    mapex["foo"] = mylist1; 
    list<string> mylist2; 
    mylist2.push_back("item 3"); 
    mylist2.push_back("item 4"); 
    mylist2.push_back("item 5"); 
    mapex["bar"] = mylist2; 

    for_each(mapex.begin(), mapex.end(), printPair); 
} 
27

更新(回到未來):用C++ 11範圍基於for循環 -

std::map<Key, Value> m { ... /* initialize it */ ... }; 

for (const auto &p : m) { 
    std::cout << "m[" << p.first << "] = " << p.second << '\n'; 
} 
0

您可以編寫一個非常通用的重載函數,這有兩個用途:

  1. 它適用於任何map
  2. 它允許使用<<

功能是

template<class key_t, class value_t> 
ostream& operator<<(ostream& os, const map<key_t, value_t>& m) { 
    for (typename map<key_t, value_t>::const_iterator it = m.begin(); 
      it != m.end(); it++) { 
     os << "Key: " << it->first << ", Value: " << it->second; 
    } 
    return os; 
} 

cout <<將與該<<用於typenamekey_t小號和value_t定義的任何map工作。在你的情況下,這沒有爲value_t(= list<string>)定義,所以你也必須定義它。 本着同樣的精神,你可以使用

template<class T> 
ostream& operator<<(ostream& os, const list<T>& l) { 
    for (typename list<T>::const_iterator it = l.begin(); it != l.end(); it++) { 
     os << "\"" << *it << "\", "; 
    } 
    return os; 
} 

所以,你可以:

  1. 添加這兩種功能。
  2. 在需要的地方添加原型。
  3. 使用using namespace std;(或根據需要添加std::)。
  4. 使用,例如,
    cout << mapex << endl;
    cout << li << endl;

請記住,如果沒有爲<<的只有我定義的任何其他可行的候選者(我拿不存在,否則你可能不會問這個問題),它可能會被優先超過目前的。