Wednesday, October 23, 2024

ostreambuf_iterator

 
Overview
IOStream classes provides iterators for reading and writing from streams just like a containers. ostreambuf_iterator enables writing characters to the stream.

Details
ostream iterators are output iterators that write successive characters from an output stream (such as cout).

They are constructed from a basic_ostream object, to which they become associated, so that whenever an assignment operator (=) is used on the ostream_iterator (dereferenced or not) it inserts a new element into the stream.

Syntax
The syntax is as below. 
template< class CharT = char,
          class Traits = char_traits<CharT>
class ostreambuf_iterator;

NameDescription
charTCharacter type of the associated istream object.
traitsCharacter traits of the associated istream object.

Members
 It defines following types.
member typedefinition
iterator_categoryoutput_iterator_tag
value_typecharT
difference_typevoid
pointervoid
referencevoid

Operation
An ostreambuf_iterator  as an output iterator supports operators *, ++, and =. 

ostreambuf_iterator  is a single-pass  output iterator that writes successive  characters  into the basic_ostream object for which it was constructed, using operator<<. Optional delimiter string is written to the output stream after every write operation. The write operation is performed when the iterator (whether dereferenced or not) is assigned to. Incrementing the ostreambuf_iterator   is a no-op.

In other words, unlike in a list or a vector, the iterator solely depends on the current position of the stream iterator of the stream object. 

sentry object is not constructed and destructed once per character.

In a typical implementation, the only data members of ostreambuf_iterator   are a pointer to the associated basic_ostream object.

This can be clearly seen in the example below. 
wstring msg = L"Hello, Khri$ha!";
//same as wcout << msg;
copy (begin(msg), end(msg), ostreambuf_iterator<wchar_t>(wcout));



Functionality
Constructors
NameDescription
  1. ostream_iterator( ostream_type& s)
  2. ostream_iterator( streambuf_type *buf);
  1. Constructs from a stream
  2. Constructs from a streambuf;
Example:
//1
wstring msg = L"Hello, Khri$ha!";
//same as wcout << msg;
copy (begin(msg), end(msg), ostreambuf_iterator<wchar_t>(wcout));

//2
wstringstream ss;
//same as wstringstream ss(L"Hello, Khri$ha!")

copy (begin(msg), end(msg), ostreambuf_iterator<wchar_t>(ss.rdbuf()));
wcout << endl << ss.str();
ostreambuf_iterator (const ostreambuf_iterator& x)
copy constructor
Constructs an ostreambuf iterator, with the same ostreambuf reference.

Example:
ostreambuf_iterator<char> it(cout), it2(it);
//prints kr
*it='k';
it2='r';

Overloaded operators
NameExample
ostreambuf_iterator& operator*() 
Does nothing.

  1. ostreambuf_iterator& operator++()
  2. ostreambuf_iterator& operator++(int)
Does nothing.
ostreambuf_iterator& operator=(const T& value) Writes to the stream.

Example:
ostreambuf_iterator<char> it(cout);
//prints kr
*it='k';
it2='r';


No comments:

Post a Comment