class template
<iterator>
std::ostreambuf_iterator
template <class charT, class traits=char_traits<charT> >
class ostreambuf_iterator;
Output stream buffer iterator
Ostreambuf iterators are output iterators that write sequentially to an stream buffer.
They are constructed from a basic_streambuf object open for writing, to which they become associated.
It is defined with a behavior 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;
};
| |
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
*this
.
operator++(int)
- Does nothing. Returns
*this
.
- failed
- Returns whether a previous writing operation failed (i.e. basic_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> // std::cin, std::cout
#include <iterator> // std::ostreambuf_iterator
#include <string> // std::string
#include <algorithm> // std::copy
int main () {
std::string mystring ("Some text here...\n");
std::ostreambuf_iterator<char> out_it (std::cout); // stdout iterator
std::copy ( mystring.begin(), mystring.end(), out_it);
return 0;
}
| |
Output: