473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ostreams and vector<vector>s

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
Jul 22 '05 #1
2 1813
"Russ Ford" <ru******@shaw.ca> wrote in message
news:uP0%c.314688$M95.132113@pd7tw1no...
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?


This is a common question. A simple googling (http://groups.google.com) of
the comp.lang.c++.* groups for ostream_iterator provides many explanations
(e.g. Adrian Sandor's post and news_user's post).

--
David Hilsee
Jul 22 '05 #2

"Russ Ford" <ru******@shaw.ca> wrote in message
news:uP0%c.314688$M95.132113@pd7tw1no...
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 << ">";
}


Reece Dunne's Output Formatters library, up for review for inclusion in boost
begining Sept 12, handles this type of problem nicely, with the capacity much
customization.

If you are interested, the library is available in the boost sandbox
(http://www.boost.org/more/mailing_lists.htm#sandbox) . The docs are here:
http://tinyurl.com/4mu39.

Jonathan
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on the elements of A and B, i.e. I want to do this:
1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
5
by: Ahmad | last post by:
Hi all, I have written a simple c++ app, as I'm still learning c++. The thing works flawlessly on VC++6, but just doesn't work on g++. The transliterate function which causes the problem is...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
3
by: Dave Rahardja | last post by:
Where is it specified that data storage for <vector> must be contiguous? i.e., Assuming v is a vector of some size, &v == &v + 1; -dr
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public : .. int getNumber(){ return num_;}
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.