33

私はmap次のように宣言しました:

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

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

4

7 に答える 7

38

アップデート(バック・トゥ・ザ・フューチャー):C ++ 11の範囲ベースのforループ– </ p>

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

for (const auto &p : m) {
    std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
于 2014-12-03T17:50:35.070 に答える
26

表示方法によって異なりますが、いつでも簡単に繰り返すことができます。

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;
}
于 2009-06-30T12:49:50.820 に答える
12

次のことを試してみます

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);
}
于 2009-06-30T12:49:44.097 に答える
5

私はここで少し話題から外れています...

デバッグのためにマップコンテンツをダンプしたいと思います。次のgdbリリース(バージョン7.0)には、gcclibstdc++がstlprettyプリンターを提供するために使用する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"
  }
}

わーい!

于 2009-06-30T13:28:12.223 に答える
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);
}
于 2013-11-22T10:36:46.490 に答える
1

非常に一般的なオーバーロードされた関数を作成できます。これは、次の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 <<sおよび。に対して定義されmapているすべてのもので機能します。あなたの場合、これは(= )に対して定義されていないので、それも定義する必要があります。同様の精神で、あなたは使用することができます<<typenamekey_tvalue_tvalue_tlist<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;

定義したばかりのsの実行可能な候補が他にある場合<<(私はそこにないと思いますが、そうでない場合はこの質問をしない可能性があります)、現在の候補よりも優先される可能性があることを覚えておいてください。

于 2016-10-12T13:00:41.343 に答える
0

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

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のコード

于 2019-03-21T09:24:36.527 に答える