![]() |
VOOZH | about |
The STL is a very powerful library in C++. It is strongly built on the principles of template programming.
The STL library has three main components :
All three components are so designed that they confirm to the principles of data abstraction. Thus any object which holds data and behaves like a container, is a container. Similarly, any iterator which sweeps through the elements in a container is an iterator.
If an iterator can be used to access elements of a data container, then what about streams? In keeping with the design, Streams too are data containers and so C++ provides us with iterators to iterate over the elements present in any stream. These iterators are called Stream Iterators. To use these iterators the iterator header file must be included.
Stream iterators are either input stream iterator or output stream iterator. The classes for these iterators are istream_iterator and ostream_iterator. These iterators behave like input iterators and output iterators respectively .
istream_iterator
Class Definition for istream_iterator
namespace std {
template < typename T, typename charT=char,
typename traits=char_traits <charT> >
class istream_iterator;
}
Syntax :
istream_iterator<T>(stream) T: Template parameter for specifying type of data stream: The object representing the stream
ostream_iterator<T>(stream, delim). stream: The object representing the stream. T: Template Parameter for data type in the stream delim: An optional char sequence that is used to separate the data items while displaying them.
Note :
ostream_iterator
Class Definition for ostream_iterator
namespace std {
template < typename T, typename charT=char,
typename traits=char_traits <charT> >
class ostream_iterator;
}
Syntax :
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); first: Input Iterator to the first element in source from where elements need to be copied. last: Input Iterator to the last element in source till where elements need to be copied. result: Output iterator to the first element in the destination container to where elements will copied. Return Value: Iterator pointing to the last element that was copied to the destination.
The second and third template arguments have a default values assigned to them. We only need to specify the first template parameter which specifies the type of data present in the stream i.e whether the stream contains integers, floats or strings.
Examples :
istream_iterator cin_it(cin) is an iterator for the stream cin. ostream_iterator cout_it(cout, " ") is an iterator for the stream cout. ostream_iterator cout_it(cout) is an iterator for stream cout, with no delimiter.
Importance of Stream Iterators
The advantage of stream iterators is that they provide a common interface to access elements in I/O stream, file streams and also other stream to external physical devices.
Example 1
Input: 1 2 3 4 5 Output: 1 2 3 4 5
Example 2
Contents of File "input_file.txt": quick brown fox jumps over the lazy dog Output: brown dog fox jumps lazy over quick the
Example 3:
Input: 1 4 3 2 6 8 31 52 Output: 2 4 6 8 52
References:
istream_iterator
ostream_iterator