マップのコンテンツをコンソールに表示するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1063453

  •  21-08-2019
  •  | 
  •  

質問

私は持っています map 次のように宣言されます。

map < string , list < string > > mapex ; list< string > li;

上記のマップに格納されているアイテムをコンソールに表示するにはどうすればよいですか?

役に立ちましたか?

解決

まあ、それはあなたがそれらを表示する方法に依存しますが、あなたは常にそれらを簡単に繰り返すことができます:

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

他のヒント

更新(戻る未来へ):C ++ 11の範囲ベースのforループと -

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

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

私は次のことを試してみた。

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);
}

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

私は、デバッグ用のマップの内容をダンプしたいと思います。私は次のgdbのリリース(バージョン7.0)はSTLかなりのプリンタを提供するために、GCCのlibstdc ++が使用するPythonインタプリタに組み込まれていますことを言及したいです。ここでは、あなたの場合の例です。

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

イェーイ!

他の形態、使用して<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);
}

非常に汎用的なオーバーロードされた関数を作成できます。これは、次の 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 << どれでも動作します map 誰のために << のために定義されています typenames key_t そして value_t. 。あなたの場合、これは定義されていません 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 つの関数を追加します。
  2. 必要に応じてプロトタイプを追加します。
  3. 使用 using namespace std; (または追加 std:: 必要に応じて)。
  4. 使用例:
    cout << mapex << endl;
    cout << li << endl;

他に有力な候補者がいる場合は、 <<は定義されたばかりです (定義されていないと私は考えています。そうでなければ、おそらくこの質問はしないでしょう)。それは現在の定義よりも優先される可能性があります。

ご利用いただければ C++11 特徴、それで私は思います 範囲ベースの for ループ で提案されているように 常磁性クロワッサンの答え 最も読みやすいオプションを提供します。ただし、 C++17 が利用できる場合は、それらのループを以下と組み合わせることができます。 構造化バインディング を使用する必要がなくなったため、可読性がさらに向上します。 first そして second メンバー。特定のユースケースの場合、私のソリューションは次のようになります。

std::map<std::string, std::list<std::string>> mapex;
mapex["a"] = { "1", "2", "3", "4" };
mapex["b"] = { "5", "6", "7" };

for (const auto &[k, v] : mapex) {
    std::cout << "m[" << k.c_str() << "] =";
    for (const auto &s : v)
        std::cout << " " << s.c_str();
    std::cout << std::endl;
}

出力:

m[a] = 1 2 3 4
m[b] = 5 6 7

Coliruのコード

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top