GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::ostreambuf_iterator

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<iterator>

  • iterator
  • iterator_traits
  • functions

    • advance
    • back_inserter
    • C++11
      begin
    • distance
    • C++11
      end
    • front_inserter
    • inserter
    • C++11
      make_move_iterator
    • C++11
      next
    • C++11
      prev
  • iterator categories

    • bidirectional_iterator_tag
    • forward_iterator_tag
    • input_iterator_tag
    • output_iterator_tag
    • random_access_iterator_tag
  • predefined iterators

    • back_insert_iterator
    • front_insert_iterator
    • insert_iterator
    • istream_iterator
    • istreambuf_iterator
    • C++11
      move_iterator
    • ostream_iterator
    • ostreambuf_iterator
    • reverse_iterator
  • Reference
  • <iterator>
  • ostreambuf_iterator
class template
<iterator>

std::ostreambuf_iterator

template <class charT, class traits=char_traits<charT> >  class ostreambuf_iterator;
Output stream buffer iterator
  • iterator
  • ostreambuf_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;
  
};

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 types

memberdefinition in ostreambuf_iteratordescription
streambuf_typebasic_streambuf<charT,traits>Type of the associated basic_streambuf object
ostream_typebasic_ostream<charT,traits>
iterator_categoryonput_iterator_tagOutput iterator
value_typevoid
char_typecharTType of the characters handled by the associated stream buffer
traits_typetraitsCharacter traits for associated stream buffer
int_typetraits::int_typeInt type to represent character values or end-of-file
difference_typevoid
pointervoid
referencevoid

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:

Some text here...


See also

istreambuf_iterator
Input stream buffer iterator (class template)
output_iterator_tag
Output iterator category (class)
ostream
Output Stream (class)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.