ostreams and vector<vector>s

Discussion in 'C++' started by Russ Ford, Sep 6, 2004.

  1. Russ Ford

    Russ Ford Guest

    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
    Russ Ford, Sep 6, 2004
    #1
    1. Advertising

  2. Russ Ford

    David Hilsee Guest

    "Russ Ford" <> 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
    David Hilsee, Sep 6, 2004
    #2
    1. Advertising

  3. "Russ Ford" <> 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
    Jonathan Turkanis, Sep 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    3,013
    John Harrison
    Jul 15, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    659
    Roger Leigh
    Jan 19, 2004
  3. pmatos
    Replies:
    6
    Views:
    23,790
  4. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  5. Thormod Johansen

    Output using ostreams on embedded hardware

    Thormod Johansen, Apr 17, 2007, in forum: C++
    Replies:
    2
    Views:
    295
    Michael DOUBEZ
    Apr 19, 2007
Loading...

Share This Page