Introduction to Iterators in C++

Last Updated : 20 Jan, 2026

An iterator is an object like a pointer that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1,2,3,4,5};
    
    // Get the iterator of first element of the vector
    vector<int>::iterator it = v.begin();
    
    // Print the content of location which is 
    // pointed by iterator (it)
    cout << *it;
    return 0;
}

Output
1

Explanation: The above example demonstrates accessing the first element of a vector container by first obtaining its iterator (it). Then dereference this iterator with the asterisk (*) operator, which outputs the value 1.

Syntax

cType::iterator iteratorName;

where,

  • cType: Container type.
  • iteratorName: Named assign to the iterator.

We can use autokeyword to skip to write container type because auto keyword automatically detected container type itself.

auto iteratorName;

Types of Iterators

All iterators do not have similar functionality, depending upon the functionality of iterators they can be classified into five categories:

  1. Input Iterator
  2. Output Iterator
  3. Forward Iterator
  4. Bidirectional Iterator
  5. Random-Access Iterator
random_access
Venn Diagram for iterators

Iterator

Description

Input

It is a read only iterator but not modified the value using input iterator.

Output

Output iterator is used to assign the values.

Forward

It is the combination of input and output iterator means for both access and assigning the values.

Bidirectional

This iterator can move in both direction either in forward or backward. Alos it's have all functionalities of forward iterators.

Random-Access

Random-access iterators are iterators that can be used to access the element at the distance from the element they pointed to. And it's had all functionalities of bidirectional iterators.

Not all of these iterators are supported by all containers in STL. Different containers support different iterators, like vectors support random-access iterators, while lists support bidirectional iterators. The whole list is as given below:

CONTAINERTYPES OF ITERATOR SUPPORTED
VectorRandom-Access
ListBidirectional
DequeRandom-Access
MapBidirectional
MultimapBidirectional
SetBidirectional
MultisetBidirectional
StackNo iterator Supported
QueueNo iterator Supported
Priority-QueueNo iterator Supported

The following diagram shows the difference in their functionality with respect to various operations that they can perform.

ITERATORSACCESSREADWRITEITERATECOMPARE
Input->=*i++==, !=
Output*i=++
Forward->=*i*i=++==, !=
Bidirectional=*i*i=++, --==, !=
Random-Access->, [ ]=*i*i=++, --, +=, -=, ==, +, -==, !=, <, >, <=, >=

To know more, refer to the article - Iterators in C++

Iterators vs Pointers

The main differences between Iterators and Pointers are given in the below table:

FeatureIteratorsPointers
DefinitionAbstractions that allow traversal over containers.Memory addresses that store the location of another variable.
UsageUsed with STL containers (e.g., vector, list, map).Used for direct memory manipulation.
FlexibilityCan work with different types of containers.Only works with raw memory and arrays.
OperationsSupport operations like increment (++), decrement (--), and dereferencing (*).Can perform arithmetic operations (+, -, ++, --).
SafetySafer, as they provide bounds checking in STL containers.Prone to segmentation faults due to out-of-bounds access.
Container CompatibilityCan be used with STL containers efficiently.Works with raw arrays and dynamically allocated memory.
PerformanceSlightly slower than pointers due to additional overhead.Faster as they directly access memory.

Advantages of Iterators

There are following advantages of iterators listed below:

  • Safe and Simple Traversal: Enable sequential access to container elements without managing indices or container boundaries.
  • STL Algorithm Compatibility: Work seamlessly with standard algorithms such as sort, find, count, and accumulate, independent of container type.
  • Multiple Iterator Categories: Provide different capabilities through iterator types such as input, forward, bidirectional, and random-access iterators.
  • Support for Reverse Traversal: Allow backward traversal of containers using reverse iterators without modifying container structure.
Comment