【发布时间】:2018-10-23 03:28:48
【问题描述】:
我这样定义一个列表:
std::list < pair<string, int> > token_list;
我想打印出所有列表元素,所以我把它写出来:
std::copy(std::begin(token_list),
std::end(token_list),
std::ostream_iterator<pair<string, int> >(std::cout, " "));
但我一直收到此错误:
error 2679:binary "<<"the operator (or unacceptable conversion) that accepts the right operand oftype (or unacceptable)
在 Visual Studio 中。我该如何解决它,或者有没有其他方法可以打印出列表中的所有对?
【问题讨论】:
-
This shows how to define an input operator for a
std::pair。您需要一个 输出 运算符,但同样的特殊条件也适用。 -
考虑使用
std::vector而不是std::list。它通常会在现代硬件上表现得更好。