By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,122 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,122 IT Pros & Developers. It's quick & easy.

Why no operator << for stl containers.

P: n/a
gg
If I have the following class -

template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};

If operator << is provided for stl containers then I can easily
implement dump method as,

os << obj_;

Do I need to provide my own operator << for stl containers?

Why can't stl provide it, as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.

Thanx
gg

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
gg wrote:
If I have the following class -

template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};

If operator << is provided for stl containers then I can easily
implement dump method as,

os << obj_;

Do I need to provide my own operator << for stl containers?
Yes.
Why can't stl provide it,
Because there was no pressing need, I guess. To know for sure, ask in
comp.std.c++ -- they answer the "why" questions.
as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


Yes, but what to put between them? Try

std::copy(obj_.begin(), obj_.end(),
std::ostream_iterator<T>(os, " "));

V
Jul 23 '05 #2

P: n/a
gg wrote:

Why can't stl provide it, as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


copy(cont.begin(), cont.end(),
ostream_iterator<element-type>(cout, " "));

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3

P: n/a
gg
My problem is that the parameter T for my template class can be
anything from int, string, stl containers, user defined objects.

I need a generalized solution for dumping all these types.

copy(cont.begin(), cont.end(),
ostream_iterator<element-type>*(cout, " "));

won't work for other types that can be passed.

Jul 23 '05 #4

P: n/a
gg wrote:
My problem is that the parameter T for my template class can be
anything from int, string, stl containers, user defined objects.

I need a generalized solution for dumping all these types.

copy(cont.begin(), cont.end(),
ostream_iterator<element-type>*(cout, " "));

won't work for other types that can be passed.


So, define your own operator<< which will use that 'copy' and replace
'element-type' with 'T'.
Jul 23 '05 #5

P: n/a
This might work for some collections:

template < typename Coll >
std::ostream & operator<< ( std::ostream & os, const Coll & coll )
{
typedef typename Coll::value_type value_type;
std::copy( coll.begin(), coll.end(),
std::ostream_iterator<value_type>( os, " " ) );
return os;
}

Of course this restricts you to using space as the deilmiter. It won't
work for map (no operator<< for pair),
and you can't define one for pair (for some reason ostream_iterator
demands the operator<< overload to be
in the same namespace as the class in which the value_type is defined).

You could write your own ostream_iterator and istream_iterator, and you
could even write them to get
ostream_iterator not to put the delimiter at the end of the last
element (You could also write in a custom
opener/delimiter/closer). I think boost have done something like this
already.

Jul 23 '05 #6

P: n/a
In message <jA*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> writes
gg wrote:
If I have the following class -
template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};
If operator << is provided for stl containers then I can easily
implement dump method as,
os << obj_;
Do I need to provide my own operator << for stl containers?
Yes.
Why can't stl provide it,


Because there was no pressing need, I guess. To know for sure, ask in
comp.std.c++ -- they answer the "why" questions.
as it is has to simply iterate over all the
elements in the container and call operator << for each of the element
in it.


Yes, but what to put between them?


And *around* them? It's not essential, but operator<< is more useful for
serialisation if there's a paired operator>> to read back what you've
just written.
Try

std::copy(obj_.begin(), obj_.end(),
std::ostream_iterator<T>(os, " "));


--
Richard Herring
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.