IOStream classes provides iterators for reading and writing from streams just like a containers. ostream_iterator enables writing objects to the stream.
Details
ostream iterators are output iterators that write successive elements 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.
Optionally, a delimiter can be specified on construction. This delimiter is written to the stream after each element is inserted.
Syntax
The syntax is as below.
template< class T, class CharT = char, class Traits = char_traits<CharT> class ostream_iterator;
Name | Description |
---|---|
T | Represents Element type for the iterator: The type of elements written to the stream |
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 | T |
difference_type | void |
pointer | void |
reference | void |
Operation
An ostream_iterator as an output iterator supports operators *, ++, and =.
ostream_iterator is a single-pass output iterator that writes successive objects of type T 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 ostream_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 constructed and destructed once per character.
In a typical implementation, the only data members of ostream_iterator are a pointer to the associated basic_ostream object and a pointer to the first character in the delimiter string.
This can be clearly seen in the example below.
vector<int> v(10),v2; //v:{10,11,12,13,14,15,16,17,18,19 } iota(begin(v),end(v),10); stringstream ss; copy ( v.begin(), v.end(), ostream_iterator<int> (ss," ") ); //v2:{10,11,12,13,14,15,16,17,18,19 } copy ( istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(v2)); //prints 10 11 12 13 14 15 16 17 18 19 copy ( v.begin(), v.end(), ostream_iterator<int> (cout," ") );ostream_iterator<char> it(cout), it2(it); //prints kr *it='k'; it2='r';
Functionality
Constructors
Name | Description |
---|---|
ostream_iterator( ostream_type& s , const char_type* delimiter = nullptr) | Constructs the iterator with stream as the associated stream, by storing the address of stream. 1) Uses delim as the delimiter. 2) Uses a null pointer as the delimiter. If delimiter is specified, and is not a null pointer, it is inserted after every element is inserted in the stream. vector<int> v(10);//v:{10,11,12,13,14,15,16,17,18,19 } iota(begin(v),end(v),10); |
ostream_iterator (const ostream_iterator& x) | copy constructor Constructs an ostream iterator, with the same ostream reference. Example: ostream_iterator<char> it(cout), it2(it); //prints kr *it='k'; it2='r'; |
Overloaded operators
Name | Example |
---|---|
ostream_iterator& operator*() | Does nothing. |
| Does nothing. |
ostream_iterator& operator=(const T& value) | Writes to the stream. Example: ostream_iterator<char> it(cout); //prints kr *it='k'; it2='r'; |
No comments:
Post a Comment