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

MAP包含值作为列表+如何在C++中打印

c++
  •  4
  • sap  · 技术社区  · 15 年前

    我有一个映射,其中字符串作为键,文件名列表作为值。 前任: Map(firstDir, list(file1,file2,file3))

    {
        cout << "Key: " << pos->first << endl;
        cout << "Value:" << pos->second << endl;
    }
    

    但是如果 pos->second 包含一个列表,如何显示?

    7 回复  |  直到 15 年前
        1
  •  7
  •   Armen Tsirunyan    15 年前

    超载 operator << 用于列表

    std::ostream& operator << (std::ostream& out, std::list<ListElemType> const& lst)
    {
       for(std::list<ListElemType>::iterator it = lst.begin(); it != lst.end(); ++it)
       {
           if(it != lst.begin())
              out << /*your delimiter*/;  
           out << *it;
       }
       return out;
    }
    

    cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl; 
    
        2
  •  2
  •   Benoît photo_tom    15 年前

    用Boost.Foreach怎么样?

    #include <boost/foreach.hpp>
    
    {
        cout << "Key: " << pos->first << endl;
        cout << "Values:" << endl;
        BOOST_FOREACH(std::string const& file, pos->second)
        {
          cout << "\t" << file << endl;
        } 
    }
    
        3
  •  2
  •   Björn Pollex    15 年前

    你首先要决定的是你想如何显示列表?用逗号分隔还是用新行中的每个条目分隔?然后,可以重载字符串列表的流输出运算符:

    std::ostream & operator<<(std::ostream & stream, const std::list<std::string> & object) {
        std::copy(object.begin(), object.end(), std::ostream_iterator<std::string>(std::cout, ", ")
    }
    

    std::list<std::string> 到任何输出流,它将打印由逗号分隔的列表值。

        4
  •  2
  •   Roger Pate Roger Pate    15 年前

    使用为容器重载流插入器的库,例如 my example :

    void example(MapType const &m) {
      using namespace kniht::container_inserters;  // must be enabled in this scope
      MapType::const_iterator x = m.begin();
    
      cout << *x << '\n';  // can print the pair directly
    
      cout << "Key: " << x->first << '\n';  // or format it yourself
      cout << "Value: " << x->second << '\n';
      // output for a list: [a, b, c]
    }
    

    你可以从我的 header 或者简单地将其复制到其他地方(它是自包含的,但是有其他实用程序)。

        5
  •  1
  •   Volodymyr Bochko    8 年前

    因为您使用的是STL,所以打印这种结构的最简单方法是下一种:

    #include <iostream> 
    #include <map>
    #include <list>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        map<string, list<string>> sample;
    
       ... //fill the map
    
        for (auto itr : sample){
            cout << itr.first << ":\t" << endl;
            for (auto innerItr : sample.second)
                cout << innerItr << " ";
            cout << endl;
        }
    }
    
        6
  •  0
  •   CashCow    15 年前

    所以创建自己的包装类。现在,因为它是您的包装器,所以您可以在上面重载operator<<而不会干扰外部命名空间。

    您可以在其构造函数中使用boost::range,或者使用模板化的being/end序列或模板化的集合。

    也可以将分隔符作为参数。如果你愿意,你可以有合理的默认值,

    std::cout << MySequenceFormatter( seq.begin(), seq.end(), delim, startofSeq, endOfSeq ) << std::endl;
    

    在输出映射时,可以在其间使用boost::transform_迭代器在遍历序列时转换每个std::pair。然后你也可以拥有自己的成对格式化程序。 实际上,您可以对任何希望使用自定义方法打印项本身的序列执行此操作。

        7
  •  0
  •   Valentin Heinitz    15 年前

    海峡前进型(--:

    #include <map>
    #include <list>
    #include <string>
    #include <iostream>
    #include <sstream> // only for generating testdata
    
    typedef std::list<std::string> TStringList;
    typedef std::map<std::string, TStringList> TStringListMap;
    
    TStringListMap myMap;
    
    int main()
    {
      // Generating testdata
      for (int i=0;i<10;i++)
      {
        std::stringstream kstr;
        kstr << i;
        std::string key = kstr.str();        
        for (int ii=0;ii<=i;ii++)
        {
          std::stringstream vstr;
          vstr << ii;
          myMap[key].push_back( vstr.str() );
        }  
      }
    
      //Print map
      for ( TStringListMap::const_iterator it = myMap.begin(), end = myMap.end(); it != end; ++it )
      {
        std::cout << it->first<< ": ";
        for( TStringList::const_iterator lit = it->second.begin(), lend = it->second.end(); lit != lend; ++lit )
        {
          std::cout << *lit << " ";
        }
        std::cout << std::endl;
      }
      return 0;
    }
    

    0: 0
    1: 0 1
    2: 0 1 2
    3: 0 1 2 3
    4: 0 1 2 3 4
    5: 0 1 2 3 4 5
    6: 0 1 2 3 4 5 6
    7: 0 1 2 3 4 5 6 7
    8: 0 1 2 3 4 5 6 7 8
    9: 0 1 2 3 4 5 6 7 8 9