Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
iterator
advance
back_inserter
distance
front_inserter
inserter
iterator
iterator_traits
iterator categories:
BidirectionalIterator
ForwardIterator
InputIterator
OutputIterator
RandomAccessIterator
predefined iterators:
back_insert_iterator
front_insert_iterator
insert_iterator
istreambuf_iterator
istream_iterator
ostreambuf_iterator
ostream_iterator
reverse_iterator


ostreambuf_iterator

class template
<iterator>
template <class charT, class traits=char_traits<charT> >
  class ostreambuf_iterator;

Output stream buffer iterator

Ostreambuf iterators are a special output iterator class designed to write successive elements to a stream buffer (streambuf or another basic_streambuf class).

They are constructed from a basic_streambuf object open for writing, to which they become associated.

It is defined with an operation similar to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template <class charT=char, class traits=char_traits<charT> >
  class ostreambuf_iterator :
    public iterator<output_iterator_tag, charT,
                    typename traits::off_type, charT*, charT&>
{
public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef typename traits::int_type int_type;
  typedef basic_streambuf<charT,traits> streambuf_type;
  typedef basic_ostream<charT,traits> ostream_type;
  ostreambuf_iterator(ostream_type& s) throw(): sbuf_(s.rdbuf()) { }
  ostreambuf_iterator(streambuf_type* s) throw(): sbuf_(s) { }
  ostreambuf_iterator& operator= (charT c)
  { if (!failed()) last=sbuf_->sputc(c); return *this; }
  ostreambuf_iterator& operator*() { return *this; }
  ostreambuf_iterator& operator++() { return *this; }
  ostreambuf_iterator& operator++(int) { return *this;}
  bool failed() const throw() { return last==traits::eof(); }
private:
  streambuf_type* sbuf_;
  charT last;
  
};


Template parameters

charT
Character type. This is the first template parameter in the basic_streambuf object.
traits
Character traits. This is the second template parameter in basic_streambuf object.

Member functions

constructor
ostreambuf_iterator objects are constructed from either a basic_streambuf object or a basic_ostream object.
operator=
Writes an element into the stream buffer.
operator*
Returns the current value in the stream buffer.
operator*
Does nothing. Returns a reference to the object.
operator++
Does nothing. Returns a reference to the object.
failed
Returns whether a previous writing operation failed (i.e. streambuf::sputc returned traits::eof() in a previous call to operator=).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ostreambuf_iterator example
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main () {
  string mystring ("Some text here...\n");
  ostreambuf_iterator<char> out_it (cout); // stdout iterator
  copy ( mystring.begin(), mystring.end(), out_it);
  return 0;
} 


Possible output:

Some text here...

See also