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

C++ Input Iterator

C++ Input Iterator

  • An iterator that is basically being used to read the values from the container is known as the Input Iterator in C++.
  • In order to retrieve the value from the container an input iterator needs to be dereferenced.
  • Dereferencing the input iterator does not alter the value of a container.
  • Input iterator is known as a one-way iterator.
  • The main point about the input iterator is that this can be incremented, but cannot be decremented.
  • These are the operators that can be used for an input iterator like equal operator(==)decrement operator(--), dereference operator(*), not equal operator(!=),and Increment operator(++)
  • Istream is known as the producer of the input iterator.
  • In input iterators all the iterators are valid, for example the Forward Iterator, bidirectional iterator and random access iterator.

Operations Performed on the Input Iterator

Here are the list of the operations that are generally performed on the Input iterators:

Property Valid Expressions
An input iterator is basically a copy-assignable, copy-constructible, and destructible. X b(a); b= a;
An input iterator can generally be compared by using a equality or inequality operator. a==b; a!=b;
An input iterator can generally be dereferenced. *a;
An input iterator can generally be incremented. ++a;

In the above mentioned table, 'X' is of input iterator type whereas 'a' and 'b' are the objects of an iterator type.

Features of Input iterator

Here are the astonishing features of the input iterator depicted below:

Equality/Inequality operator:

Users can compare the bidirectional iterator just by using an equality or inequality operator. Whenever these both iterators point towards the exact same position then it is said that these iterators are equal, only when the given condition is fulfilled.

Here is an example of this feature that will make you understand the meaning of it:

#include <iostream>  
#include<vector>  
#include<iterator>  
using namespace std;  
int main()  
{  
    vector<int> vect{10,20,30,40,50};  
    vector<int>::iterator itr1,itr2;  
    itr1=vect.begin();  
    itr2=vect.begin()+1;  
    if(itr1==itr2)
	{
    std::cout << "Both the iterators are equal" << std::endl;  
    }
	if(itr1!=itr2)
	{	
    std::cout << "Both the iterators are not equal" << std::endl;  
    }
	return 0;  
}  
Output: Both the iterators are not equal

Dereferencing an iterator:

The programmers can generally dereference an input iterator just by the use of a dereference operator(*).

Here is an example of this feature that will make you understand the meaning of it:

#include <iostream>  
  #include<vector>  
 #include<iterator>  
 using namespace std;  
int main()  
  {
   vector<int> vect{10,20,30,40};  
   vector<int>::iterator itr;  
   itr = vect.begin();  
   cout<<*itr;  
  return 0; 
}  
Output: 10

Swappable:

The input iterators can be swapped provided that the two iterators are pointing two different locations.

Here is an example of this feature that will make you understand the meaning of it:

#include <iostream>  
#include<vector>  
#include<iterator>  
using namespace std;  
int main()  
{  
    vector<int> vect{10,20,30,40};  
    vector<int>::iterator itr,itr1,temp;  
    itr = vect.begin();  
    itr1 = vect.begin()+1;  
    temp=itr;  
    itr=itr1;  
    itr1=temp;  
    cout<<*itr<<" ";  
    cout<<*itr1;  
    return 0;  
}  
Output: 20 10

No Sidebar ads