Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Iterators: The Container-Algorithm Glue

โญโญ intermediateโณ 25 min read๐Ÿ“ Lesson 97 of 204

begin/end semantics, the four iterator categories, and why std::sort refuses a list.

An iterator is the glue between containers and algorithms: an object that points into a sequence and can move to the next element. Every container exposes begin() and end(), and end() is one-past-the-last โ€” a marker, never a value to dereference.

#include <vector>
#include <iostream>

std::vector<int> v{10, 20, 30};
for (auto it = v.begin(); it != v.end(); ++it) {
    std::cout << *it << " ";           // dereference reads the element
}
*v.begin() = 11;                        // non-const iterators can write

The iterator categories, from weakest to strongest:

  • input/output โ€” single pass (stream iterators)
  • forward โ€” re-read, multi-pass (forward_list, unordered_map)
  • bidirectional โ€” plus --it (list, map, set)
  • random access โ€” plus it + n, it1 - it2, it[n] (vector, deque, array)

Category decides which algorithms you may call: std::sort needs random access, so it works on vector but not on list (which offers its own l.sort() member). const containers hand back const_iterators โ€” you can read, you cannot write.