• An iterator is an object like a pointer that points to an element inside the container.
  • Iterator can use to move through the contents of the container.
  • We can access the content at that particular location using them and it can be visualized as something similar to a pointer pointing to some location.
  • It plays a critical role while connecting algorithm with containers along with the manipulation of data stored inside the containers.
  • Pointer is a most obvious form of an iterator.
  • In an array pointer can point to elements and using increment operator (++) it can iterate through them.
  • All iterator doesn’t have similar functionality as that of pointers.
  • Depending upon the functionality of iterators they can be classified into five categories, they are:
    • Input iterator
    • Output iterator
    • Forward iterator
    • Bidirectional iterator
    • Random access iterator

Input iterator

  • They have limited functionality and weakest of all iterators.
  • In single-pass algorithms it can be used.
  • Those algorithms which process the container sequentially, such that no element is accessed more than once.

Output iterator

  • It is also like input operators, they are also very limited in their functionality and can only be used in single-pass algorithm, but not for accessing elements, but for being assigned elements.

Forward iterator

  • It is higher in hierarchy than input iterator and output iterator and contain all the features present in these two iterators
  • As name suggests, they can also only move in a forward direction and that too one step at a time.

Bidirectional iterator

  • It has all the features of forward iterators along with the fact that they overcome the drawback of forward iterators, in both the directions they can move, hence their name is Bidirectional iterators.

Random Access iterator

  • It is the most powerful iterators and they are not limited to moving sequentially, as their name suggests, they can randomly access any element inside the container.

Sample Code

#include <iostream>

    #include <vector>

    using namespace std;

    int main()

    {

            // Declaring a vector

            vector<int> v = { 1, 2, 3 };




           // Declaring an iterator

           vector<int>::iterator i;




           int j;




           cout << "Without iterators = ";

     

           // Accessing the elements without using iterators

           for (j = 0; j < 3; ++j)

        {

          cout << v[j] << " ";

        }




          cout << "\nWith iterators = ";

     

          // Accessing the elements using iterators

         for (i = v.begin(); i != v.end(); ++i)

      {

          cout << *i << " ";

      }




         // Adding one more element to vector

         v.push_back(4);




         cout << "\nWithout iterators = ";

     

         // Accessing the elements without using iterators

        for (j = 0; j < 4; ++j)

     {

         cout << v[j] << " ";

     }




         cout << "\nWith iterators = ";

     

        // Accessing the elements using iterators

       for (i = v.begin(); i != v.end(); ++i)

    {

        cout << *i << " ";

    }




    return 0;

  }

Output

 

Categorized in: