C++ 映射包含列表形式的值+;如何在C+中打印+;

C++ 映射包含列表形式的值+;如何在C+中打印+;,c++,C++,我有一个地图,以字符串作为键,以文件名列表作为值。 示例:Map(firstDir,list(file1,file2,file3)) 我知道通过使用下面的代码,我可以打印字符串形式的值 { cout << "Key: " << pos->first << endl; cout << "Value:" << pos->second << endl; } { 可以使用Boost.Foreach吗 #

我有一个地图,以字符串作为键,以文件名列表作为值。 示例:
Map(firstDir,list(file1,file2,file3))

我知道通过使用下面的代码,我可以打印字符串形式的值

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

可以使用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;
    } 
}
#包括
{

cout重载
运算符您必须决定的第一件事是如何显示列表?用逗号或新行中的每个条目分隔?然后,您可以重载字符串列表的流输出运算符:

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::ostream&operator使用一个为容器重载流插入器的库,例如:

void示例(MapType const&m){
必须在此范围内启用使用命名空间knit::container_inserters;//的
MapType::const_迭代器x=m.begin();

cout不过,您希望能够创建一个模板,该模板可以输出任何可打印序列,即可打印项目的序列。您还希望能够自定义序列的开始、结束和定界方式

因此,创建您自己的包装器类。现在,因为它是您的包装器,所以您可以重载运算符向前版本(-:


由于您使用的是STL,打印此类结构的最简单方法是下一种:

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
地图样本;
…//填图
用于(自动itr:示例){

还有一种更优雅的方法是将
std::copy
std::ostream\u迭代器一起使用。您也可以为此指定一个自定义分隔符。@Space\u C0wb0y:您的方法更具stl风格,但我不会说它优雅,因为我认为它可读性不强。@Space\u C0wb0y:问题是,使用copy和ostream迭代器时,del“IMATER将在每个元素之后应用,而不是在它们之间。因此,我认为你是对的,”罗杰-派特已经指出了我的答案。我不明白为什么人们会给答案高分数,这会使事情变得比他们复杂。C++中的另一个概念是:操作符重载。为什么要在答案中引入新概念,I?答案应该是imho:“将pos->second”作为std::list的一个实例。(下面是我的版本)“虽然逗号没有分开,但它们终止了每个项目:
”a,b,“
对于包含两项的列表。@rubenvb:因为没有0x,它比BOOST\u FOREACH要麻烦得多。@Roger:是的,我被宠坏了:)只使用GCC 4.5和MSVC 2010…我忘了以前需要写些什么恐怖的东西。@Roger:有没有计划建议你的库升级?:)我的意思是,从外观上看,它至少和boost一样值得升级assign@Armen当前位置我不会称之为图书馆(整个回购协议是随机组合的);我真的以为Boost对此有所贡献,但找不到。@Roger:我不是指回购协议,我是指广义上的项目
cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl; 
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, ", ")
}
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]
}
std::cout << MySequenceFormatter( seq.begin(), seq.end(), delim, startofSeq, endOfSeq ) << std::endl;
#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
#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;
    }
}