R
Russ Ford
I'm trying to overload the << operator to output vectors, as follows:
template <class T>
ostream& operator << (ostream& o, const vector<T>& v)
{
o << "< ";
copy (v.begin(), v.end(), ostream_iterator<T>(o, " ");
return o << ">";
}
This works well, except for outputting a vector<vector> where compile errors
occur. The STL reference says that the ostream_iterator will work for any
type T where cout << T is defined. Doesn't the above define cout << for a
vector?
If I define like this instead,
template <class T>
ostream& operator << (ostream& o, const vector<T>& v)
{
o << "< ";
vector<T>::const_iterator i;
for (i=v.begin(); i<v.end(); ++i) cout << *i << " ";
return o << ">";
}
it works fine. What gives?
Russ
template <class T>
ostream& operator << (ostream& o, const vector<T>& v)
{
o << "< ";
copy (v.begin(), v.end(), ostream_iterator<T>(o, " ");
return o << ">";
}
This works well, except for outputting a vector<vector> where compile errors
occur. The STL reference says that the ostream_iterator will work for any
type T where cout << T is defined. Doesn't the above define cout << for a
vector?
If I define like this instead,
template <class T>
ostream& operator << (ostream& o, const vector<T>& v)
{
o << "< ";
vector<T>::const_iterator i;
for (i=v.begin(); i<v.end(); ++i) cout << *i << " ";
return o << ">";
}
it works fine. What gives?
Russ