打印字符串向量的地图

时间:2013-11-05 04:03:33

标签: c++ c++11 vector map cout

尝试打印map<string, vector<string>>,但我一直收到错误:

prob2.cc: In function âvoid printMap(const std::map<std::basic_string<char>, std::vector<std::basic_string<char> > >&)â: prob2.cc:42:36: error: cannot bind âstd::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}â lvalue to âstd::basic_ostream<char>&&â In file included from /opt/centos/devtoolset-1.1/root/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40:0,

我不知道它意味着什么,不能将ostream左值绑定到ostream&amp;&amp;在文件iostream中。

void printMap(const map<string, vector<string>> &mymap)
{
for(auto const& i : mymap)
  cout << i.first << endl << i.second << endl;  //wanting to print out the vector of
}                                              //strings belonging to each string key

1 个答案:

答案 0 :(得分:1)

vector<string>没有流输出运算符(i.second的类型)。你需要迭代向量。如果你经常这样做,你可以定义一个运算符:

ostream & operator<<( ostream &s, const vector<string>& v )
{
    // TODO: You choose how you want it to look.
    return s;
}