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.
#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.
| Operation | Description |
|---|---|
| *it | Access the element pointed to by the iterator |
| ++it | Move to the next element |
| --it | Move to the previous element (if supported) |
| it1 == it2 | Compare two iterators |
| it1 != it2 | Check 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:

Iterator | Description |
|---|---|
Input iterators are read-only iterators used to access elements sequentially. | |
Output iterators are write-only iterators used to assign values. | |
Forward iterators combine the functionality of input and output iterators. | |
Bidirectional iterators extend forward iterators by allowing movement in both directions. | |
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.
| 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 |
Operations Supported by Different Iterator Categories
The following table summarizes the operations supported by each category of iterator:
| Iterator Type | Access | Read | Write | Iteration | Comparison |
|---|---|---|---|---|---|
| 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.