.\" ident @(#)ostream_iterator.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH ostream_iterator 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2ostream_iterator\fP \ - Stream iterators allow for use of iterators with ostreams and istreams. They allow generic algorithms to be used directly on streams. .SH SYNOPSIS .br #include .br template > .RE .RS 0 class ostream_iterator .RE .RS 1 : public iterator; .SH DESCRIPTION Stream iterators use the standard iterator interface for input and output streams. The class ostream_iterator writes elements to an output stream. If you use the constructor that has a second \f2char *\fP argument, then that string is written after every element (the string must be null-terminated). Since an ostream iterator is an output iterator, it is not possible to get an element out of the iterator. You can only assign to it. .SH INTERFACE .RE .RS 0 template > .RE .RS 0 class ostream_iterator .RE .RS 3 : public iterator .RE .RS 0 { .br public: .RE .RS 1 typedef T value_type; .br typedef charT char_type; .br typedef traits traits_type; .br typedef basic_ostream ostream_type; .RE .RS 0 .RE .RS 3 ostream_iterator(ostream&); .br ostream_iterator (ostream&, const char*); .br ostream_iterator (const .RE .RS 11 ostream_iterator >&); .RE .RS 4 ~ostream_itertor (); .RE .RS 3 ostream_iterator >& .RE .RS 8 operator=(const T&); .RE .RS 3 ostream_iterator >& .RE .RS 8 operator* () const; .RE .RS 3 ostream_iterator >& .RE .RS 8 operator++ (); .RE .RS 3 ostream_iterator > .RE .RS 8 operator++ (int); .RE .RS 1 }; .SH TYPES .RE .RS 0 value_type; .RE .RS 3 Type of value to stream in. .RE .br char_type; .RE .RS 3 Type of character the stream is built on. .RE .br traits_type; .RE .RS 3 Traits used to build the stream. .RE .br ostream_type; .RE .RS 3 Type of stream this iterator is constructed on. .RE .SH CONSTRUCTORS .br ostream_iterator (ostream& s); .RE .RS 3 Constructs an_ostream_iterator on the given stream. .RE .br ostream_iterator (ostream& s, const char* delimiter); .RE .RS 3 Constructs an_ostream_iterator on the given stream. The null terminated string \f2delimiter\fP is written to the stream after every element. .RE .br ostream_iterator (const ostream_iterator& x); .RE .RS 3 Copy constructor. .RE .SH DESTRUCTORS .br ~ostream_iterator (); .RE .RS 3 Destroys an object of class \f2ostream_iterator\fP. .RE .SH OPERATORS .br const T&
operator= (const T& value); .RE .RS 3 Shift the value \f2T\fP onto the output stream. .RE .br const T& ostream_iterator& .br operator* (); .br ostream_iterator& .br operator++(); .br ostream_iterator .br operator++ (int); .RE .RS 3 These operators do nothing. They simply allow the iterator to be used in common constructs. .RE .SH EXAMPLE .RE .RS 1 #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .br int main () .RE .RS 1 { .RE .RS 3 // .br // Initialize a vector using an array. .br // .RE .RS 2 int arr[4] = { 3,4,7,8 }; .br int total=0; .br deque d(arr+0, arr+4); .RE .RS 3 // .br // stream the whole vector and a sum to cout .br // .RE .RS 2 copy(d.begin(),d.end()-1, .RE .RS 8 ostream_iterator(cout," + ")); .RE .RS 2 cout << *(d.end()-1) << " = " << .RE .RS 7 accumulate(d.begin(),d.end(),total) << endl; .RE .RS 2 return 0; .RE .RS 1 } .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2deque >\fP instead of: \f2deque\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO istream_iterator, Iterators