2009-06-30 1 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; 
} 
+0

ポストインクリメント? – triclosan

+0

@triclosan:この場合はあまり重要ではありません。それとも私はあなたを誤解したことがありますか? – Skurmedel

+0

余分な一時的なオブジェクトの作成にポストインクリメントのゲインを使用する – triclosan

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

最初の行に 'の前に 'unqualified-idが必要です。' 'void dump_list(const std :: list &l){'何かを含める必要がありますか? –

+0

dump_listには、次のようになります。 std :: vector :: const_iterator it = l.begin(); for(it; it!= l.end(); ++ it){ std :: cout << * it << std :: endl; } と勘違いしていません!それ以外の場合は非常に役に立ちました /ありがとう! – Kahin

4

を私は少しオフトピックですここ...

私はデバッグのためにマップの内容をダンプしたいと思う。次のgdbリリース(バージョン7.0)には、gcc libstdC++でstl prettyプリンタを提供するために使用されるpythonインタープリタが組み込まれています。ここにあなたのケース

(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" 
    } 
} 

イェーイになり

#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 
    } 

の例です! <algorithm>

使用
2

別の形態:

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

非常に一般的なオーバーロードされた関数を書くことができます。これは、2つの目的に適しています。

  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 <<<<typename S key_tvalue_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;

(私はそこではありません取る、そうでなければ、おそらくこの質問をしません)だけで定義された<<のための他の任意の実行可能な候補がある場合、それは優先することを忘れないでください現在のものより

関連する問題