
Module: Standard C++ Library Library: Iterators
ostream_iteratoriterator
Stream iterators allow for use of iterators with ostreams and istreams. They allow generic algorithms to be used directly on streams.
#include <ostream>
namespace std {
template <class T, class charT,
class traits = char_traits<charT> >
class ostream_iterator;
}
Stream iterators use the standard iterator interface for input and output streams.
The class template ostream_iterator writes elements to an output stream. If you use the constructor that has a second 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 dereference values through the iterator. You can only assign to it.
namespace std {
template <class T, class charT = char,
class traits = char_traits<charT> >
class ostream_iterator:
public iterator<output_iterator_tag,void,void,void,void>
{
public:
typedef T value_type;
typedef charT char_type;
typedef traits traits_type;
typedef basic_ostream<charT,traits> ostream_type;
ostream_iterator(ostream_type&);
ostream_iterator(ostream_type&, const char_type*);
ostream_iterator(const ostream_iterator&);
ostream_iterator& operator=(const T&);
ostream_iterator& operator*() const;
ostream_iterator& operator++();
ostream_iterator operator++(int);
};
}
char_type;
Type of character the stream is built on.
traits_type;
Traits used to build the stream.
ostream_type;
Type of stream this iterator is constructed on.
ostream_iterator (ostream_type& s);
Constructs an ostream_iterator on the given stream.
ostream_iterator (ostream_type& s,
const char_type* delimiter);
Constructs an ostream_iterator on the given stream. The null terminated string delimiter is written to the stream after every element.
ostream_iterator (const ostream_iterator& x);
Copy constructor.
const T&
operator= (const T& value);
Insert value into the associated output stream x by calling x << value.
const T& ostream_iterator& operator* (); ostream_iterator& operator++(); ostream_iterator operator++ (int);
These operators do nothing. They simply allow the iterator to be used in common constructs.
#include <iterator>
#include <numeric>
#include <deque>
#include <iostream>
using namespace std;
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,char>(cout," + "));
cout << *(d.end()-1) << " = " <<
accumulate(d.begin(),d.end(),total) << endl;
return 0;
}
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 24.5.2
Copyright (c) 1994-2006 Rogue Wave Software, a Quovadx Division.
Licensed under the Apache License, Version 2.0.
Contact Rogue Wave about documentation or support issues. You can also seek help from other developers through the Apache stdcxx community (see below).