【发布时间】:2015-09-16 18:37:27
【问题描述】:
我有以下代码可以漂亮地打印通用向量 -:
// print a vector
template<typename T1>
std::ostream& operator <<( std::ostream& out, const std::vector<T1>& object )
{
out << "[";
if ( !object.empty() )
{
std::copy( object.begin(), --object.end(), std::ostream_iterator<T1>( out, ", " ) );
out << *--object.end(); // print the last element separately to avoid the extra characters following it.
}
out << "]";
return out;
}
如果我尝试从中打印嵌套向量,则会收到编译器错误。
int main()
{
vector<vector<int> > a;
vector<int> b;
// cout << b ; // Works fine for this
cout << a; // Compiler error
}
我正在使用带有 -std=c++14 标志的 GCC 4.9.2。
编译器给出的错误信息是-:
no match for 'operator<<' (operand types are 'std::ostream_iterator<std::vector<int>, char, std::char_traits<char>::ostream_type {aka std::basic_ostream<char>}' and 'const std::vector<int>')
【问题讨论】:
标签: c++ vector compiler-errors pretty-print