Input Iterators in C++

In the case of various input operations in C++, we use input iterators. Each value is pointed by the iterator once and the iterator gets incremented.

There are 5 main types of iterators present in C++ programming:

  1. Output Iterator
  2. Forward Iterator
  3. Bidirectional Iterator
  4. Random Acess Iterator
  5. Input Iterator

Input iterator is the weakest and the simplest amongst all the iterators present because the user finds it very easy to use them and the user cannot achieve much through it.

Features Of Input Iterators in C++

The different features of Input Iterators are:

  • Usability: The programmer uses input iterators only with the algorithms which help us to go to different locations at most once.
  • Comparison:  We can compare it with other iterators. Hence, if two iterators point to the same location then they are considered to be equal.
  • Incremental:  We can also increment input iterators using  ++() so that they point to the next element in the sequence.
  • Swappable: The programmer can swap the values pointed by these iterators.

Sample Program: input iterators

Example program for input iterator is as follows:

#include <iostream>  
#include<vector>  
#include<iterator>  
using namespace std;  
int main()  
{  
    vector<int> v{1,2,3,4,5};  
    vector<int>::iterator itr,itr1;  
    itr=v.begin();  
    itr1=v.begin()+1;  //Value gets incremented
    if(itr==itr1)  
    std::cout << "Both the iterators are equal" << std::endl;  
    if(itr!=itr1)  
    std::cout << "Both the iterators are not equal" << std::endl;  
    return 0;  
}

Limitations of Input Iterators

The limitations of Input Iterators are as follows:

  • They are not able to assign a value.
  • The Programmers cannot decrement it.
  • We can only use it with equality operators and not other relational operators.
  • Input Iterators cannot use it with Arithmetic Operators.

Also, check out Code Bloating in C++ with Examples.

Leave a Reply

Your email address will not be published. Required fields are marked *