Jon Stewart on 15 Aug 2003 17:48:21 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] container streaming, sigh


> Hm, I was hoping to somehow bind directly to operator<< (though it won't have
>  the endl I want).


Dude, boost::lambda to the rescue:

#include <boost/lambda...> // look it up

ostream& operator<<(ostream& str, const vector<string>& rhs)
{
	for_each(rhs.begin(), rhs.end(), str << _1 << ends);
	return str;
}

It's the canonical boost::lambda example.


OR... if, GOD KNOWS WHY, boost::lambda doesn't work out for you...

ostream& operator<<(ostream& str, const vector<string>& rhs)
{
	std::copy(ostream_iterator<string>(str),
		ostream_iterator<string>(),
		back_inserter(rhs));
	return str;
}

The above isn't tested and is basically going off memory. YMMV.

I think there's an option to the ostream_iterator constructor for a 
delimiter so you could have it print out the newline.

Note that when in a loop like this, you probably want std::ends not 
std::endl. std::endl performs a flush. That should be a little gotcha' in 
a book somewhere.



Jon
-- 
Jon Stewart                                 Advanced Los Angeles C++
stew1@xxxxxxxxxxx                           http://www.alacpp.org
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp