🧩 C++ STL & Data Structures
Estimated reading: 3 minutes 271 views

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

TypeDescription
input_iteratorRead-only, forward-only
output_iteratorWrite-only, forward-only
forward_iteratorRead/write, forward-only
bidirectional_iteratorMove forward and backward
random_access_iteratorSupports indexing, pointer arithmetic

Common STL Containers and Their Iterator Types

ContainerIterator Type
vectorRandom access
dequeRandom access
listBidirectional
set/mapBidirectional
unordered_*Forward only

Iterator Functions

FunctionDescription
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 :
Share

C++ Iterators

Or Copy Link

CONTENTS
Scroll to Top