C++ Iterators β Traverse and Manipulate Containers Seamlessly
Introduction β Why Use Iterators in C++
In C++, iterators provide a uniform way to access elements in STL containers. They act like pointers and allow navigation, traversal, and modification of container elements without exposing internal structure. Iterators are essential for making STL containers, algorithms, and loops work together efficiently.
In this guide, youβll learn:
- What iterators are and how they work
- Different types of iterators and their roles
- How to use iterators with STL containers
- Best practices for safe and efficient iteration
What Are Iterators in C++?
Iterators are objects that point to elements within STL containers. They support incrementing, dereferencing, and comparison, allowing you to loop through container elements.
Include the relevant container headers:
#include <vector>
#include <list>
#include <map>
#include <iterator>
Code Examples β With Output
Iterating a Vector Using Iterators
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
return 0;
}
Output:
10 20 30
Iterating with Range-Based For Loop (C++11+)
for (int x : v)
cout << x << " ";
Equivalent to iterator-based loop internally.
Types of Iterators
| Type | Description |
|---|---|
input_iterator | Read-only, forward-only |
output_iterator | Write-only, forward-only |
forward_iterator | Read/write, forward-only |
bidirectional_iterator | Move forward and backward |
random_access_iterator | Supports indexing, pointer arithmetic |
Common STL Containers and Their Iterator Types
| Container | Iterator Type |
|---|---|
vector | Random access |
deque | Random access |
list | Bidirectional |
set/map | Bidirectional |
unordered_* | Forward only |
Iterator Functions
| Function | Description |
|---|---|
begin() | Returns iterator to the first element |
end() | Returns iterator past the last element |
rbegin() | Reverse begin iterator |
rend() | Reverse end iterator |
cbegin() | Constant iterator to begin |
cend() | Constant iterator to end |
Using Iterators with Algorithms
#include <algorithm>
vector<int> v = {1, 3, 2};
sort(v.begin(), v.end()); // Uses iterators internally
Iterators power all STL algorithms like sort, find, accumulate, and copy.
Best Practices & Tips
Use auto (C++11+) to simplify iterator syntax
Prefer range-based loops when no modification is needed
Do not dereference end() β itβs past-the-last element
Use constant iterators (const_iterator) when elements shouldnβt be modified
Use Cases for Iterators
Traversing Containers β Lists, maps, vectors, sets
Search and Filter β Use with find, remove_if, copy_if
Modifying Data β In-place operations inside algorithms
Bulk Processing β Pair with back_inserter for dynamic inserts
Custom Loops β Implement your own looping logic on containers
Summary β Recap & Next Steps
Key Takeaways:
- Iterators offer a standard, efficient way to traverse and modify containers
- Each STL container has its own iterator type based on capabilities
- Iterators integrate seamlessly with STL algorithms
Real-World Relevance:
C++ iterators are foundational in algorithms, data structure manipulation, and high-performance STL-based applications.
Next Steps:
Explore C++ STL Algorithms to perform sorting, searching, and transformations using iterators.
FAQ β C++ Iterators
Are iterators and pointers the same?
No, but they behave similarly. Iterators can work on more complex containers like lists or maps.
What happens if I increment end()?
Undefined behavior. Never increment or dereference end().
Can I use auto with iterators?
Yes. auto it = container.begin(); is standard practice since C++11.
Can I modify container elements using iterators?
Yes, unless you’re using a const_iterator.
Whatβs the use of reverse_iterator?
Allows traversal from the end of the container to the beginning.
Share Now :
