C++: STL Iterators

Iterators are pointer like object which are used to point at the memory addressess of STL containers.

Properties of Iterator 

We can dereference an iterator in order to access the value to which it refers. That is, if p is an iterator, *p is defined.

We can assign one iterator to another. That is, if p and q are iterators, the expression p = q is defined.
We can compare one iterator to another for equality. That is, if p and q are iterators, the expressions p == q and p != q are defined.

We can move an iterator through all the elements of a container. This can be satisfied by defining ++p and p++ for an iterator p .

Operations of iterators

1. begin() :-  This function returns an iterator to the first element of the container.

2. end() :- This function returns an iterator to end of the container.

3. advance() :- This function increment the iterator position to the specified position mentioned in argument.

4. next() :- This function returns an iterator pointing to the element after 'n' position mentioned in the argument.

5. prev() :- This function returns an iterator pointing to the element 
before 'n' position mentioned in the argument.

Program using these functions

  #include <iostream>
#include <vector>
#include <iterator>

int main()
  std::vector<int> v = {1,2,3,4,5};

  //declaring iterator to the vector
  std::vector<int>::iterator itr;

  //displaying velements of vector using begin() and end()
  std::cout<<"The elements are : ";
  for(itr = v.begin(); itr < v.end(); itr++)
      std::cout<< *itr <<" ";


  itr = v.begin();

  //incrementing iterators position using advance()
  advance(itr, 3);

  std::cout<<"Iterator pointing to after incrementing : "<< *itr <<"\n";

  std::vector<int>::iterator itr1 = v.begin();
  std::vector<int>::iterator itr2 = v.end();

  //using next() and prev() to generate new iterator
  auto itn = next(itr1 ,2);
  auto itp = prev(itr2, 2);

  std::cout<<"itn is pointing to : "<< *itn <<" itp is pointing to : "<< *itp <<"\n";

  return 0;

Snapshot of the Output:

Types of Iterators: 

The five kinds of iterator are input iterator, output iterator, forward iterator, bidirectional iterator and random access iterator.

Input Iterator

Dereferencing an input iterator allow a program to read the value from container, but it does not allow program to change the value.
So algorithms that require input iterator are algorithms that do not change values held in the container.   

Output Iterator

Dereferencing an output iterator allow a program to change the value but not to read it. Similarly cout can modify the stream of characters sent to the display, but it can’t read what is onscreen.

We can use an input iterator for single-pass, read-only algorithms and an output iterator for single-pass, write-only algorithms.

Forward Iterator

Like input and output iterators, forward iterators use only the ++ operators for navigating through a container. So a forward iterator can only go forward through a container one element at a time. However, unlike input and output iterators, it necessarily goes through a sequence of values in the same order each time we use it. Also, after we increment a forward iterator, we can still dereference the prior iterator value, if we have saved it, and get the same
value. These properties make multiple-pass algorithms possible.

A forward iterator can allow us to both read and modify data, or it can allow us just to read it:

int * itr;      // read-write iterator
const int * it; // read-only iterator

Bidirectional Iterator

Suppose you have an algorithm that needs to be able to traverse a container in both directions.
For example, a reverse function could swap the first and last elements, increment the pointer to the first element, decrement the pointer to a second element, and repeat the process. A bidirectional iterator has all the features of a forward iterator and adds support for the two decrement operators (prefix and postfix).

Random Access Iterator

Some algorithms, such as standard sort and binary search, require the ability to jump directly to an arbitrary element of a container. This is termed random access, and it requires a random access iterator. This type of iterator has all the features of a bidirectional iterator, plus it adds operations (such as pointer addition) that support random access and relational operators for ordering the elements.

Iterator Capabilities

 Follow us on


You may also like:
C++: Pointers 
C++: Stack implementation using LinkedList (Data Structure) 
C++: Understanding Template
C++: Queue implementation using Linked List (Data Structure) 
C++: Singly Linked List using Template (Data Structure)