std::istream_iterator and std::ostream_iterator are stream iterators provided by the C++ Standard Library that allow streams to be treated like containers. They provide a convenient way to read data from input streams and write data to output streams using STL algorithms.
- std::istream_iterator is used to read values from an input stream.
- std::ostream_iterator is used to write values to an output stream.
Stream Iterators
Stream iterators are special iterators that work with input and output streams. They act as a bridge between STL algorithms and stream objects, allowing operations such as copying, filtering, and transforming data using standard algorithms.
To use stream iterators, include the <iterator> header file:
#include <iterator>
There are two types of stream iterators:
std::istream_iterator
std::istream_iterator is an input iterator that reads values sequentially from an input stream.
- Supports only sequential reading.
- Behaves like an input iterator.
- Provides a special end-of-stream iterator.
- Automatically advances after reading.
Class Template
template <
class T,
class CharT = char,
class Traits = std::char_traits<CharT>
>
class istream_iterator;
Syntax
istream_iterator<T> itr(stream);
where:
- T is the type of data to read.
- stream is an input stream object.
std::ostream_iterator
std::ostream_iterator is an output iterator that writes values sequentially to an output stream.
- Supports only writing operations.
- Behaves like an output iterator.
- Can be used with STL algorithms such as copy().
- Supports optional delimiters.
Class Template
template <
class T,
class CharT = char,
class Traits = std::char_traits<CharT>
>
class ostream_iterator;
Syntax
ostream_iterator<T> itr(stream);
ostream_iterator<T> itr(stream, delimiter);
where:
- T is the type of data to write.
- stream is an output stream object.
- delimiter is an optional separator inserted after each element.
Examples of Stream Iterators
Example 1: Copy Input Stream to Output Stream
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
// Get input stream and end of stream iterators
istream_iterator<int> cin_it(cin);
istream_iterator<int> eos;
// Get output stream iterators
ostream_iterator<int> cout_it(cout, " ");
// We have both input and output iterators, now we can treat them
// as containers. Using copy function we transfer data from one
// container to another.
// Copy elements from input to output using copy function
copy(cin_it, eos, cout_it);
return 0;
}
Input: 1 2 3 4 5
Output: 1 2 3 4 5Explanation: The copy() algorithm reads integers from cin using istream_iterator and writes them to cout using ostream_iterator.
Example 2: Read Data from File and Sort It
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main()
{
// Define a vector to store the strings received from input
vector<string> strings_v;
// Define the filestream object used to read data from file
ifstream fin("input_file.txt");
// Get input stream and end of stream iterators
istream_iterator<string> fin_it(fin);
istream_iterator<string> eos;
// Get output stream iterators
ostream_iterator<string> cout_it(cout, " ");
// Copy elements from input to vector using copy function
copy(fin_it, eos, back_inserter(strings_v));
// Sort the vector
sort(strings_v.begin(), strings_v.end());
// Copy elements from vector to output
copy(strings_v.begin(), strings_v.end(), cout_it);
return 0;
}
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: Filter and Print Even Numbers
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
// Define a vector to store the even integers received from input
vector<int> vi;
// Get input stream and end of stream iterators
istream_iterator<int> cin_it(cin);
istream_iterator<int> eos;
// Get output stream iterators
ostream_iterator<int> cout_it(cout, " ");
// Copy even integer elements from input to vector using for_each function
for_each(cin_it, eos, [&](int a) {
if (a % 2 == 0) {
// if a is even push it to vector
vi.push_back(a);
}
});
// Sort the vector
sort(vi.begin(), vi.end());
// Copy elements from vector to output
copy(vi.begin(), vi.end(), cout_it);
return 0;
}
Input: 1 4 3 2 6 8 31 52
Output: 2 4 6 8 52 Difference Between istream_iterator and ostream_iterator
| Feature | istream_iterator | ostream_iterator |
|---|---|---|
| Iterator Category | Input Iterator | Output Iterator |
| Purpose | Read from stream | Write to stream |
| Supports Reading | Yes | No |
| Supports Writing | No | Yes |
| End Iterator | Yes | No |
| Typical Algorithms | find, count, copy | copy, transform, move |