CPP Tutorial

CPP examples programs

CPP Iterators

CPP facilitates another unique feature same as that of pointers, known as iterators. Iterators in CPP are used to access the container elements by traversing through one element to another element.

Syntax 1:

<Container_Type> :: iterator;

Syntax 2:

<Container_Type> :: const_iterator;

 

Iterator Functions:

There are mainly two functions that are used for an iterator in CPP. These are:

  • begin():

This function is used to return an iterator pointing to the first element of the container.

  • end():

This function is used to return an iterator pointing to the last element of the container.

 

Types of Iterators:

There are mainly five types of iterators including: input iterator, output iterator, forward iterator, bidirectional iterator and random access iterator, which are discussed in detail below.

  • Input Iterator:

An iterator which is used to access the elements from the container is called an input iterator. An input iterator does not modify the value of a container and is thus called a read only iterator. There are various operators which are used for an input iterator including: Increment operator(++), Equal operator(==), Not equal operator(!=) and Dereference operator(*).

  • Output Iterator:

An iterator which is used to modify the value of a container is called an output iterator. An output iterator is a write only iterator. There are various operators which are used for an output iterator including: Increment operator(++) and Assignment operator(=).

  • Forward Iterator:

An iterator which is used to both read and write to a container can be a forward iterator. A forward iterator is a multi-pass iterator. There are various operators which are used for a forward iterator including: Increment operator(++), Equal operator(=), Not equal operator(!=) and Assignment operator(=).

  • Bidirectional Iterator:

An iterator which is used to both read and write to a container is called a bidirectional iterator. A bidirectional iterator is also a multi-pass iterator. There are various operators which are used for a bidirectional iterator including: Increment operator(++), Decrement operator(–), Equal operator(=), Not equal operator(!=) and Assignment operator(=).

  • Random Access Iterator:

An iterator which is used to provide random access of an element at an arbitrary location is called a random access iterator. There are various operators which are used for a random access iterator including: Increment operator(++), Decrement operator(–), Equal operator(=), Not equal operator(!=), Assignment operator(=), pointer addition and pointer subtraction. Unlike other iterators that requires n number of steps to access an element, the random access iterator requires only a single step.

 

Importance of using Iterators:

  • While using an iterator to access the elements of a container, it is not necessary to keep track of the number of elements added at the runtime, as that in a case of using a subscript operator, thus easing the programming.
  • Iterators facilitates code reusability.
  • Iterators facilitates addition or deletion of data dynamically.
  • Iterators removes the dependency of an algorithm on the type of the container used.
  • In order to navigate through the elements of a container, iterators facilitates with a generic approach.

 

Limitations of using Iterators:

  • Iterators does not facilitate the movement from one data structure to another at the same time.
  • Iterators does not facilitate any update in the structure which is being iterated.
  • Iterators does not facilitate backtracking while processing through a list.

 

Example:

#include <iostream.h>  
#include <vector.h>  
#include <iterator.h>  
using namespace std;  
int main()  
{  
vector<int> arr{1000,5000,6000};  
vector<int>::iterator x;  
for(int i=0;i<3;i++)     	 
{  
cout<<arr[i]<<"\t";  
}  
cout<<endl;  
for(x=arr.begin();x!=arr.end();x++)  
{  
cout<<*x<<"\t";  
}  
arr.push_back(10000);  
cout<<endl;  
for(int i=0;i<4;i++)  
{  
cout<<arr[i]<<"\t";  
}  
cout<<endl;  
for(x=arr.begin();x!=arr.end();x++)  
{  
cout<<*x<<"\t";  
}  
return 0;  
}

Output

1000    5000    6000      
1000    5000    6000         
1000    5000    6000    10000           
1000    5000    6000    10000