Summary
Ostream iterators are a handy but under-utilized tool for using the STL to output containers and ranges. Here I provide an alternative, which has a more pleasing syntax.
Advertisement
An ostream iterator, allows you to use STL algorithms, such as std::copy to output a container or range to an output stream. A simple example is:
The declaration for an ostream_iterator is clunky because you to know the kind of elements it will be reading. You can make the code more readable as follows:
This is still more complex than strictly neccessary. The problem is that the ostream_iterator is a template. An alternative implementation would be to make the operator=() member function a template.
I liked your generic ostream iterator, but i felt compelled to gild the lily, so this version automatically dereferences pointers. It might not be what everybody wants all the time, but its often what i want a lot of the time!