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;
Name | Description |
---|---|
charT | Character type of the associated istream object. |
traits | Character traits of the associated istream object. |
Members
It defines following types.
member type | definition |
---|---|
iterator_category | output_iterator_tag |
value_type | charT |
difference_type | void |
pointer | void |
reference | void |
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.
A 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
Name | Description |
---|---|
|
//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
Name | Example |
---|---|
ostreambuf_iterator& operator*() | Does nothing. |
| 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