Top of document
©Copyright 1999 Rogue Wave Software

ostream_iterator


     Iterator

Summary

Stream iterators provide iterator capabilities for ostreams and istreams. They allow generic algorithms to be used directly on streams.

Contents

Synopsis

#include <iterator>
template <class T>
class ostream_iterator : public output_iterator;

Description

Stream iterators provide 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, char * argument, then that string will be 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.

Interface

template <class T>
 class ostream_iterator  :  public  output_iterator 
{

 public:
    ostream_iterator(ostream&);
    ostream_iterator (ostream&, const char*); 
    ostream_iterator (const ostream_iterator<T>&);
    ~ostream_itertor ();
    ostream_iterator<T>& operator=(const T&);
    ostream_iterator<T>& operator* () const;
    ostream_iterator<T>& operator++ ();
    ostream_iterator<T>  operator++ (int);
 };

Constructors

ostream_iterator (ostream& s);
ostream_iterator (ostream& s, const char* delimiter);
ostream_iterator (const ostream_iterator<T>& x);

Destructor

~ostream_iterator ();

Operators

const T& 
operator= (const T& value);
const T& ostream_iterator<T>& 
operator* ();
ostream_iterator<T>&  
operator++();
ostream_iterator<T>
operator++ (int);

Example

 #include <iterator>
 #include <numeric>
 #include <deque>
 #include <iostream.h>
 int main ()
 {
   //
   // Initialize a vector using an array.
   //
   int arr[4] = { 3,4,7,8 };
   int total=0;
   deque<int> d(arr+0, arr+4);
   //
   // stream the whole vector and a sum to cout
   //
   copy(d.begin(),d.end()-1,ostream_iterator<int>(cout," + "));
   cout << *(d.end()-1) << " = " <<
          accumulate(d.begin(),d.end(),total) << endl;
   return 0;
 }

Warning

If your compiler does not support default template parameters, then you need to always supply the Allocator template argument. For instance, you will need to write :

deque<int, allocator>

instead of :

deque<int>

See Also

istream_iterator, Iterators


Top of document