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 版)将有一个内置的 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"
  }
}

耶!

于 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

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

  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. 在需要的地方添加原型。
  3. 使用using namespace std;(或std::根据需要添加)。
  4. 使用,例如,
    cout << mapex << endl;
    cout << li << endl;

请记住,如果刚刚定义的 s 有任何其他可行的候选者<<(我认为没有,否则你可能不会问这个问题),它可能优先于当前的候选者。

于 2016-10-12T13:00:41.343 回答
0

如果您可以使用C++11功能,那么我认为The Paramagnetic Croissant 的答案中提出的基于范围的 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 回答