Introduction to Iterators in C++

Last Updated : 27 Jun, 2026

An iterator in C++ is an object that behaves similarly to a pointer and is used to access and traverse elements stored in STL containers. It provides a generic interface for working with different container types without exposing their internal implementation.

  • Provides a common mechanism to traverse and access elements in different STL containers.
  • Enables STL algorithms to work independently of specific container implementations.
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 iterator it points to the first element of the vector. Dereferencing the iterator using the * operator accesses the value stored at that position.

Syntax

container_type::iterator iteratorName;

where:

  • container_type is the STL container.
  • iteratorName is the name of the iterator.

Modern C++ also provides the auto keyword, which automatically deduces the iterator type:

auto it = container.begin();

Basic Iterator Operations

Iterators support several common operations for traversing containers.

OperationDescription
*itAccess the element pointed to by the iterator
++itMove to the next element
--itMove to the previous element (if supported)
it1 == it2Compare two iterators
it1 != it2Check whether two iterators are different

Types of Iterators

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

random_access
Venn Diagram for iterators

Iterator

Description

Input

Input iterators are read-only iterators used to access elements sequentially.

Output

Output iterators are write-only iterators used to assign values.

Forward

Forward iterators combine the functionality of input and output iterators.

Bidirectional

Bidirectional iterators extend forward iterators by allowing movement in both directions.

Random-Access

Random access iterators provide all capabilities of bidirectional iterators along with direct access to elements.

Iterator Support in STL Containers

Different STL containers support different iterator categories.

ContainerTypes of Iterator supported
VectorRandom-Access
ListBidirectional
DequeRandom-Access
MapBidirectional
MultimapBidirectional
SetBidirectional
MultisetBidirectional
StackNo iterator Supported
QueueNo iterator Supported
Priority-QueueNo iterator Supported

Operations Supported by Different Iterator Categories

The following table summarizes the operations supported by each category of iterator:

Iterator TypeAccessReadWriteIterationComparison
Input Iterator*it++==, !=
Output Iterator*it = value++
Forward Iterator->*it*it = value++==, !=
Bidirectional Iterator->*it*it = value++, --==, !=
Random Access Iterator->, []*it*it = value++, --, +=, -=, +, -==, !=, <, >, <=, >=

Advantages of Iterators

Iterators provide several benefits when working with STL containers:

  • Simplify container traversal without using indices.
  • Work seamlessly with STL algorithms such as sort(), find(), and count().
  • Provide different levels of functionality through various iterator categories.
  • Support forward and reverse traversal while hiding container implementation details.

Related articles

Comment