Introduction to Iterators in C++ (original) (raw)
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 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;}
`
**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:
- **Input Iterator
- **Output Iterator
- **Forward Iterator
- **Bidirectional Iterator
- **Random-Access Iterator

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:
| **CONTAINER | **TYPES OF ITERATOR SUPPORTED |
|---|---|
| Vector | Random-Access |
| List | Bidirectional |
| Deque | Random-Access |
| Map | Bidirectional |
| Multimap | Bidirectional |
| Set | Bidirectional |
| Multiset | Bidirectional |
| Stack | No iterator Supported |
| Queue | No iterator Supported |
| Priority-Queue | No iterator Supported |
The following diagram shows the difference in their functionality with respect to various operations that they can perform.
| **ITERATORS | **ACCESS | **READ | **WRITE | **ITERATE | **COMPARE |
|---|---|---|---|---|---|
| **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:
| Feature | Iterators | Pointers |
|---|---|---|
| **Definition | Abstractions that allow traversal over containers. | Memory addresses that store the location of another variable. |
| **Usage | Used with STL containers (e.g., vector, list, map). | Used for direct memory manipulation. |
| **Flexibility | Can work with different types of containers. | Only works with raw memory and arrays. |
| **Operations | Support operations like increment (++), decrement (--), and dereferencing (*). | Can perform arithmetic operations (+, -, ++, --). |
| **Safety | Safer, as they provide bounds checking in STL containers. | Prone to segmentation faults due to out-of-bounds access. |
| **Container Compatibility | Can be used with STL containers efficiently. | Works with raw arrays and dynamically allocated memory. |
| **Performance | Slightly 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.