代码之家  ›  专栏  ›  技术社区  ›  Cute

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

  •  33
  • Cute  · 技术社区  · 16 年前

    我有一个 map

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

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

    7 回复  |  直到 6 年前
        1
  •  39
  •   The Paramagnetic Croissant    11 年前

    std::map<Key, Value> m { ... /* initialize it */ ... };
    
    for (const auto &p : m) {
        std::cout << "m[" << p.first << "] = " << p.second << '\n';
    }
    
        2
  •  26
  •   Skurmedel    16 年前

    这取决于你想如何显示它们,但你总是可以很容易地迭代它们:

    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;
    }
    
        3
  •  12
  •   JaredPar    16 年前

    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);
    }
    
        4
  •  5
  •   Maik Beckmann    16 年前

    我有点跑题了。..

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

        5
  •  2
  •   mMontu    12 年前

    另一种形式,使用 <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);
    }
    
        6
  •  1
  •   sancho.s ReinstateMonicaCellio    9 年前

    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 key_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. using namespace std; (或添加 std::

    2. cout << mapex << endl;
      cout << li << endl;

    << 这是刚刚定义的(我认为没有,否则你可能不会问这个问题),它可能会优先于现在的问题。

        7
  •  0
  •   honk    6 年前

    C++11 那么,我认为 range-based for loops 如所提议的 The Paramagnetic Croissant's answer C++17 structured bindings 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;
    }
    


    Code on Coliru