Home >>C++ Standard Template Library Tutorial(STL) >C++ Output Iterator

C++ Output Iterator

C++ Output Iterator

  • An iterator that is generally used to modify the value in the container is known as the Output Iterator in C++.
  • In order to alter the value of the container output iterator is dereferenced.
  • Reading the value from the container is not permitted by the Output iterator.
  • Output iterator is generally known as the one-way and write-only iterator.
  • Output iterators in C++ can generally be incremented but cannot be decremented otherwise.
  • These are the list of the operators that can generally be used for an output iterator: assignment operator(=), increment operator(++), and decrement operator(--).

An Output Iterator generally have two main subclasses that are as depicted below:

  • insert iterator
  • ostream iterator

1. Insert Iterator

An iterator that is generally used to insert the element in a specified position is known as the insert iterator. The new element is inserted on the insert_iterator by an assignment operator at the current position.

Syntax

Here is the syntax of the input iterator:

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

Parameters

Here are the parameters of the syntax of the insert iterator:

x: The new element is generally inserted in this container.

it: An iterator object that is generally pointing towards the position that is to be modified is known as it.

Here is an example of the insert iterator that will help you grasp the more of it from a deeper level:

#include <iostream>  
#include <iterator>    
#include <vector>     
#include <algorithm>  
using namespace std;  
int main () {  
  vector<int> vect1,vect2;  
  for (int i=1; i<=5; i++)  
  {   
  vect1.push_back(i);   
  vect2.push_back(i+2);  
  }  
 vector<int>::iterator it = vect1.begin();  
  advance (it,3);  
 copy (vect2.begin(),vect2.end(),inserter(vect1,it));  
  cout<<"Elements of vect1 are :";  
  for (  it = vect1.begin(); it!= vect1.end(); ++it )  
  cout << ' ' << *it;  
  cout << '\n';  
  return 0;  
}  
Output: Elements of vect1 are : 1 2 3 3 4 5 6 7 4 5

Ostream iterator

The output iterators that are generally used to write to the output stream like cout successively are known as the ostream iterator. By the use of the basic_ostream object an ostream iterator is created. A new element is generally inserted into the output stream, whenever an assignment operator is implemented on the ostream operator.

Syntax

Here is the syntax of the ostream iterator:

template<class T, class charT=char, class traits=char_traits<charT>> class ostream_iterator; Here are the member functions of Ostream Iterator class:

  • 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);

Parameters

Here are the parameters of the syntax of the ostream iterator:

  • T: It is basically the type of the elements that is to be inserted into a container.
  • charT: It is basically the type of the elements that can be handled by the ostream.
  • traits: These are basically the character traits that the stream can handle for the elements.

Here is an example of the ostream iterator that will help you in understanding the method easily:

#include <iostream>  
#include<iterator>  
#include<vector>  
#include<algorithm>  
using namespace std;  
int main()  
{  
   vector<int> vect;  
   for(int i=1;i<=5;i++)  
   {  
       vect.push_back(i*10);  
   }  
 ostream_iterator<int> out(cout,",");  
 copy(vect.begin(),vect.end(),out);  
    return 0;  
}  
Output: 10,20,30,40,50,

Here is another simple example of the ostream iterator:

#include <iostream>  
#include<iterator>  
#include<vector>  
#include<algorithm>  
using namespace std;  
int main()  
{  
   ostream_iterator<int> out1(cout,",");  
   *out1 = 10;  
   out1++;  
   *out1 = 20;  
   out1++;  
   *out1 = 30;  
   return 0;  
}  
Output: 10,20,30

Features Of Output Iterator

Here are the excellent features that are provided by the Output iterator.

  • Equality/Inequality operator: Users cannot compare the output iterator by using an equality or inequality operator.
  • Dereferencing: For the value of an lvalue the output iterator can be generally dereferenced.
  • Incrementable: Incrimination of the output iterator can be done just by the use of an operator++ () function.

Limitations Of output Iterator

Here are the limitations that generally apply to the output Iterator:

  • Assigning but no accessing: We can assign an output iterator can be assigned as an lvalue, but it cannot be accessed as an rvalue by the programmers.
  • It cannot be decremented: We can increment the output iterator can generally be incremented by the use of operator++() function but it cannot be decremented.
  • Multi-pass algorithm: An output iterator in C++ is not allowed to be used as a multi-pass algorithm. An output iterator cannot be used to move through the container for multiple times as the output iterator is unidirectional and hence, can only move in to one direction.
  • Relational Operators: By using any of the relational operators the programmers cannot compare the output iterators.
  • Arithmetic Operators: As the output iterator can only move in the forward direction that too in a sequential manner hence, the output iterator cannot be used with the arithmetic operators.

No Sidebar ads