0

尝试打印 a 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 左值绑定到文件 iostream 中的 ostream&&。

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
4

1 回答 1

1

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

ostream & operator<<( ostream &s, const vector<string>& v )
{
    // TODO: You choose how you want it to look.
    return s;
}
于 2013-11-05T04:10:10.013 回答