Output Iterators in C++


Here we will see what are the Output iterators in C++. The Output iterators has some properties. These are like below:

  • The output iterators are used to modify the value of the containers.
  • We cannot read data from container using this kind of iterators
  • This is One-Way and Write only iterator
  • It can be incremented, but cannot be decremented.
  • There are two sub-parts of Output iterators. These are Insert Iterator and ostream iterator.

The Insert Iterator

The insert iterator is used to insert some element inside the container. The assignment operator on this type of iterator inserts new element at current position. The syntax of insert iterator is like below:

template<class Container, class Iterator>
insert_iterator<container> inserter(Container &x,Iterator it);

This iterator takes two parameters, x and it. The x is the container, on which the iterator will work. The second argument is an iterator object, which is pointing the position which is to be modified.

Example Code

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main () {
   vector<int> vec1,vec2;
   for (int i=1; i<=10; i++) { //insert elements into vectors
      vec1.push_back(i);
      vec2.push_back(i+3);
   }
   vector<int>::iterator it = vec1.begin(); //iterator works on vector1
   advance (it,5); //advance it to 5 position
   copy (vec2.begin(),vec2.end(),inserter(vec1,it));
   cout<<"Elements of vec1 are :";
   for ( it = vec1.begin(); it!= vec1.end(); ++it )
      cout << ' ' << *it;
      cout << endl;
   return 0;
}

Output

Elements of vec1 are : 1 2 3 4 5 4 5 6 7 8 9 10 11 12 13 6 7 8 9 10

The ostream Iterator

The ostream iterator is used to write to the output stream like cout. The ostream iterator can be created using basic_ostream object. When the assignment operator is used with this type of iterator, it inserts new element to the output stream. The syntax is like below.

template<class T, class charT=char, class traits=char_traits<charT>>
class ostream_iterator;

The member functions of ostream iterator class is like below.

ostream_iterator<T, charT, traits>& operator=(const T& value);
ostream_iterator<T, charT, traits>& operator*();
ostream_iterator<T, charT, traits>& operator++();
ostream_iterator<T, charT, traits>& operator++(int);

The parameters are: T. This is the type of elements that will be inserted, chart, this is the type of elements that ostream can handle, and traits. These are character traits that can be handled by the stream.

Example Code

#include <iostream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
main() {
   vector<int> vector;
   for(int i=1;i<=10;i++)
      vector.push_back(i*i); //make square and insert
      ostream_iterator<int> out(cout,",");
      copy(vector.begin(),vector.end(),out);
}

Output

1,4,9,16,25,36,49,64,81,100,

Another example,

Example Code

#include <iostream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
main() {
   ostream_iterator<int> os_out(cout,",");
   *os_out = 10;
   os_out++; //point to next
   *os_out = 20;
   os_out++;
   *os_out = 30;
}

Output

10,20,30,

Updated on: 30-Jul-2019

492 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements