Topic 65 of 91 9. C++ Standard Template Library (STL) Interactive Tutorial

C++ Iterators: Traversing STL Containers and Accessing Elements

8 mins read Last updated: 26 Sep 2026 16,905 views

Direct Answer & Core Concept

Iterators are pointer-like objects that provide a unified interface for traversing elements across diverse STL containers without exposing their underlying memory architecture. The 5 standard iterator categories are Input, Output, Forward, Bidirectional, and Random-Access (plus Contiguous in C++20).

Detailed Explanation & Mechanics

1. The Iterator Hierarchy:

Architecture Diagram
┌─────────────────────────────────────────────────────────────┐
│                    ITERATOR CATEGORIES                      │
├─────────────────────────────────────────────────────────────┤
│ • Input / Output: Single-pass read/write (std::istream_it)  │
│ • Forward: Multi-pass forward traversal (std::forward_list) │
│ • Bidirectional: Forward & Backward ++/-- (std::list, map)  │
│ • Random-Access: Arithmetic offset it += n (std::vector, dq)│
│ • Contiguous: Contiguous memory guarantee (std::vector)     │
└─────────────────────────────────────────────────────────────┘

2. Iterator Pairs:

Algorithms operate on half-open ranges [begin, end) where begin points to the first element and end points one past the last element.

Runnable Code Example: C++ Iterators

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <vector>
#include <iterator>

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

    // 1. Forward traversal using const iterator
    std::cout << "Forward Traversal: ";
    for (std::vector<int>::const_iterator it = nums.cbegin(); it != nums.cend(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << "\n";

    // 2. Reverse traversal using reverse iterator
    std::cout << "Reverse Traversal: ";
    for (auto rit = nums.rbegin(); rit != nums.rend(); ++rit) {
        std::cout << *rit << " ";
    }
    std::cout << "\n";

    // 3. Random-access iterator arithmetic
    auto it = nums.begin();
    it += 3; // Direct jump in O(1)
    std::cout << "nums[3] via iterator offset: " << *it << "\n";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why are STL ranges defined as half-open intervals `[first, last)`?

A: Half-open intervals allow empty ranges when `first == last`, provide natural loop termination (`it != last`), and simplify size calculations (`last - first`).
Q

Q: What is the difference between `iterator` and `const_iterator`?

A: An `iterator` allows reading and modifying the pointed-to element (`*it = 10;`). A `const_iterator` allows read-only access (`const T&`), preventing modification of the container element.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Prefer auto and range-based for loops over explicit verbose iterator declarations.
• Use cbegin() / cend() when iterating read-only to enforce const-correctness.
• Use std::next(it, n) and std::advance(it, n) for generic iterator movement across container types.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.