π 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 :
