ostream_iterator class template — Output iterator to write items to an ostream
template <typename T, typename charT = char, typename traits = char_traits<charT> > class ostream_iterator : public iterator<output_iterator_tag,void,void,void,void> { public: typedef charT char_type; typedef traits traits_type; typedef basic_ostream<charT,traits> ostream_type; ostream_iterator(ostream_type& s); ostream_iterator(ostream_type& s, const charT* delimiter); ostream_iterator(const ostream_iterator<T,charT,traits>& x); ~ostream_iterator( ); ostream_iterator<T,charT,traits>& operator=(const T& value); ostream_iterator<T,charT,traits>& operator*( ); ostream_iterator<T,charT,traits>& operator++( ); ostream_iterator<T,charT,traits>& operator++(int); };
The ostream_iterator
class
template wraps an output iterator around an output stream (instance
of basic_ostream
), making the
stream appear to be a sequence of items, each of type T
. For example, suppose you have a vector
of data. You can print the data, one number per line, by using an
ostream_iterator
:
std::vector<int> data; . . . // Acquire data. std::copy(data.begin( ), data.end( ), std::ostream_iterator(std::cout, "\n"));
The following are the member functions of ostream_iterator
:
ostream_iterator
(ostream_type& stream)
, ostream_iterator
(ostream_type&
stream, const
charT*
delimiter)
, ostream_iterator
(const
ostream_iterator<T,charT,traits>&
x)
Prepares to write items to stream
. If delimiter
is present, it will be
written after each item. The copy constructor copies the
reference to the stream and to the delimiter from x
.
ostream_iterator<T,charT,traits>&
operator=
(const T&
value)
Writes value
to the
stream using operator<<
. If the ostream_iterator
has a delimiter, it
is written after value
. The
return value is *this
.
ostream_iterator<T,charT,traits>&
operator*
( )
Returns *this
.
ostream_iterator<T,charT,traits>&
operator++
( )
, ostream_iterator<T,charT,traits>&
operator++
(int)
Returns *this
with no
side effects.
istream_iterator class
template, ostreambuf_iterator class
template, basic_ostream
in
<ostream>