Skip to content

Iterators: The STL Glue

Understand iterator categories, from input iterators to contiguous iterators. Learn iterator arithmetic, the begin/end convention, and how iterators connect containers to algorithms.

The Iterator Abstraction

Iterators are the glue between STL containers and algorithms. Instead of writing N algorithms for M containers (N*M implementations), the STL defines a common iterator interface. Each container provides iterators, and each algorithm operates on iterator ranges — giving N+M implementations that work together.

An iterator is an object that:
1. Points to an element in a sequence
2. Can be dereferenced (*it) to access the element
3. Can be incremented (++it) to move to the next element
4. Can be compared (it != end) to detect the end of the sequence

Different iterator categories support different sets of operations, forming a hierarchy from weakest to strongest.

Iterator Categories

The C++ iterator categories form a hierarchy (each level includes the capabilities of all levels below):

| Category | Operations | Example Containers |
|---|---|---|
| Input | Read, single-pass forward (++, *, ==) | istream_iterator |
| Output | Write, single-pass forward (++, *=) | ostream_iterator, back_inserter |
| Forward | Read/write, multi-pass forward | forward_list, unordered_set |
| Bidirectional | Forward + backward (--) | list, set, map |
| Random Access | Bidirectional + jumps (+n, -n, [], <) | deque |
| Contiguous (C++17) | Random Access + elements are adjacent in memory | vector, array, string |

Algorithms declare the minimum category they require. std::sort needs random access iterators, so it works with vector but not list (which provides its own sort() member function).

The begin/end Convention

Every STL container provides begin() and end() — a half-open range [begin, end) where end points one past the last element. Free functions std::begin() and std::end() work with containers, C-arrays, and any type that provides member .begin() and .end().

begin_end.cpp
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
    std::vector<int> vec = {5, 3, 1, 4, 2};

    // Member functions
    auto first = vec.begin();   // points to 5
    auto last  = vec.end();     // points past 2

    // Free functions — preferred for generic code
    auto first2 = std::begin(vec);
    auto last2  = std::end(vec);

    // Works with C-arrays too
    int arr[] = {10, 20, 30};
    std::sort(std::begin(arr), std::end(arr));

    // const iterators — read-only access
    auto cit = vec.cbegin();     // const_iterator
    // *cit = 99;                // ERROR: cannot modify through const_iterator

    // Reverse iterators — iterate backward
    for (auto rit = vec.rbegin(); rit != vec.rend(); ++rit) {
        std::cout << *rit << ' ';  // 2, 4, 1, 3, 5 (reversed, pre-sort was applied)
    }
    std::cout << '\n';
}

Iterator Utilities: next, prev, advance, distance

The header provides utility functions that work across all iterator categories, choosing the most efficient implementation automatically.

iterator_utils.cpp
#include <iterator>
#include <vector>
#include <list>
#include <iostream>

int main() {
    std::vector<int> vec = {10, 20, 30, 40, 50};

    // std::next — returns iterator n positions forward (default 1)
    auto it = std::next(vec.begin(), 2);   // points to 30
    std::cout << *it << '\n';              // 30

    // std::prev — returns iterator n positions backward
    auto it2 = std::prev(vec.end());       // points to 50
    std::cout << *it2 << '\n';             // 50

    // std::advance — modifies iterator in place (no return)
    auto it3 = vec.begin();
    std::advance(it3, 3);                  // now points to 40
    std::cout << *it3 << '\n';             // 40

    // std::distance — number of increments from first to last
    auto d = std::distance(vec.begin(), vec.end());  // 5
    std::cout << "Distance: " << d << '\n';

    // These work with non-random-access iterators too
    std::list<int> lst = {1, 2, 3, 4, 5};
    auto lit = std::next(lst.begin(), 2);  // O(n) for list, O(1) for vector
    std::cout << *lit << '\n';             // 3

    auto ld = std::distance(lst.begin(), lst.end());  // O(n) for list
    std::cout << "List distance: " << ld << '\n';     // 5
}

Insert Iterators and Stream Iterators

Insert iterators allow algorithms to insert into containers rather than overwriting existing elements. Stream iterators adapt I/O streams to the iterator interface.

insert_stream_iterators.cpp
#include <iterator>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>

int main() {
    std::vector<int> src = {1, 2, 3, 4, 5};
    std::vector<int> dst;

    // back_inserter — calls push_back on each assignment
    std::copy(src.begin(), src.end(), std::back_inserter(dst));
    // dst: {1, 2, 3, 4, 5}

    // front_inserter — calls push_front (works with deque, list)
    // inserter — calls insert at a given position

    // ostream_iterator — write to an output stream
    std::copy(dst.begin(), dst.end(),
              std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';  // 1, 2, 3, 4, 5,

    // istream_iterator — read from an input stream
    std::istringstream iss("10 20 30 40");
    std::vector<int> from_stream(
        std::istream_iterator<int>(iss),
        std::istream_iterator<int>()   // end-of-stream sentinel
    );
    // from_stream: {10, 20, 30, 40}
}
Pitfall

Incrementing an iterator past end() or decrementing past begin() is undefined behavior. The same applies to dereferencing end() — it is a sentinel, not a valid element.

A common mistake is writing it + 1 without checking whether it is already at or near the end:

``cpp
auto it = vec.end();
++it; // UB — past-the-end
*it; // UB — dereferencing end()
``

Always check iterator validity before operations. Use std::next and std::prev with distance checks, or use range-based for loops that handle bounds automatically.

Best Practice

When writing generic functions that operate on sequences:

1. Accept iterator pairs (or ranges in C++20) rather than specific containers
2. Use typename or auto for the iterator type
3. Use std::distance instead of subtraction (which requires random access)
4. Use std::next/std::prev instead of +/- arithmetic
5. In C++20, prefer concepts to constrain iterator requirements:

``cpp
template
auto my_algorithm(Iter first, Iter last) { ... }
``

This ensures your code works with the widest range of containers.

Key Takeaways
  • Iterators decouple containers from algorithms — the STL's core design principle
  • Six iterator categories exist: input, output, forward, bidirectional, random access, contiguous
  • Use free functions std::begin() / std::end() for generic code that works with containers and C-arrays
  • std::next, std::prev, std::advance, std::distance work across all iterator categories
  • Insert iterators (back_inserter, front_inserter, inserter) let algorithms grow containers
  • Never dereference end() or increment past it — both are undefined behavior

Quiz — Test Your Knowledge

(15 XP)

1. Which iterator category does `std::sort` require?

2. What does `std::back_inserter(vec)` create?

3. What is the time complexity of `std::distance(first, last)` on a `std::list`?