Loading...

Iterators and Their Use in the C++ STL

Learn iterators in C++ and how they work with STL containers and algorithms to traverse and manipulate data efficiently.

The C++ Standard Library (STL) relies on a powerful abstraction called the iterator, which enables traversal over data structures. Iterators are the core of STL containers (vector, list, map, set…) and allow moving from one element to another in a unified and consistent way. This article explains iterator types, common usage patterns, and how they integrate with STL algorithms.


1) What Is an Iterator?

An iterator is an object used to access elements of a container. It behaves similarly to a pointer but is fully abstracted by the STL.

Main purposes of iterators:

Example


#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> v = {10, 20, 30};

    vector<int>::iterator it = v.begin();

    cout << *it; // 10
}

2) Types of Iterators

Different STL containers support different iterator categories.

Examples: vector → random access list → bidirectional map/set → bidirectional


3) begin() and end() Functions

All STL containers provide these two essential functions:


vector<int> v = {1, 2, 3};

for (auto it = v.begin(); it != v.end(); ++it) {
    cout << *it << " ";
}

4) Const Iterator (const_iterator)

A const iterator is used for read-only access; it cannot modify elements.


vector<int> v = {10, 20, 30};

vector<int>::const_iterator it = v.begin();

// *it = 50; // ERROR: cannot modify through const_iterator

cout << *it;

5) Reverse Iterators (rbegin & rend)

Reverse iterators allow traversing a container backward.


vector<int> v = {1, 2, 3};

for (auto it = v.rbegin(); it != v.rend(); ++it) {
    cout << *it << " ";
}
// Output: 3 2 1

6) Modifying Elements with Iterators


vector<int> v = {10, 20, 30};

for (auto it = v.begin(); it != v.end(); ++it) {
    *it *= 2;
}

for (int x : v)
    cout << x << " ";
// Output: 20 40 60

7) Iterators and STL Algorithms

All STL algorithms operate using iterators.

Using sort


#include <algorithm>

vector<int> v = {50, 20, 10};

sort(v.begin(), v.end());

Using find


auto it = find(v.begin(), v.end(), 20);

Using count


int c = count(v.begin(), v.end(), 10);

8) Iterating over map and set


#include <map>
#include <iostream>
using namespace std;

int main() {
    map<string, int> ages = {
        {"John", 25},
        {"Emily", 30},
        {"Michael", 28}
    };

    for (auto it = ages.begin(); it != ages.end(); ++it) {
        cout << it->first << " : " << it->second << endl;
    }
}

9) Deleting Elements Using Iterators

Iterators help remove specific elements efficiently.


vector<int> v = {10, 20, 30, 40};

auto it = find(v.begin(), v.end(), 20);

if (it != v.end())
    v.erase(it);

10) Advantages of Using Iterators


11) TL;DR

  • Iterator: standard way to access container elements
  • begin/end → forward iteration
  • rbegin/rend → reverse iteration
  • const_iterator → read-only iteration
  • All STL algorithms rely on iterators
  • Examples tested with Visual Studio 2022 and GCC

Related Articles