如何在控制台上显示地图的内容?

时间:2009-06-30 12:44:17

标签: c++ dictionary stl stdmap

我声明了map如下:

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

如何在控制台上显示存储在上述地图中的项目?

7 个答案:

答案 0 :(得分:36)

更新(回到将来):使用C ++ 11基于范围的for循环 -

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

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

答案 1 :(得分:23)

这取决于您希望如何显示它们,但您可以轻松地迭代它们:

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

答案 2 :(得分: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);
}

答案 3 :(得分:5)

我在这里有点偏离......

我想你想要转储地图内容以进行调试。我想提一下,下一个gdb版本(7.0版)将有一个内置的python解释器,gcc libstdc ++将使用它来提供stl漂亮的打印机。以下是您案例的示例

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

耶!

答案 4 :(得分: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);
}

答案 5 :(得分:1)

您可以编写一个非常通用的重载函数,这有两个目的:

  1. 适用于任何map
  2. 允许使用<<
  3. 功能是

    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 s <<typename定义了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. 在需要的地方添加原型。
    3. 使用using namespace std;(或根据需要添加std::)。
    4. 使用,例如,
      cout << mapex << endl;
      cout << li << endl;
    5. 请记住,如果对于刚刚定义的<<有任何其他可行的候选人(我不会这样做,否则你可能不会问这个问题),它可能优先于现在的候选人。

答案 6 :(得分:0)

如果您可以使用C++11功能,那么我认为range-based for loops中提出的The Paramagnetic Croissant's answer提供了最易读的选项。但是,如果您可以使用C++17,则可以将这些循环与structured bindings结合使用以进一步提高可读性,因为您不再需要使用textareafirst成员。对于您的特定用例,我的解决方案如下所示:

second

输出:

  

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

Code on Coliru

相关问题