0Pricing
C++ Academy · Lesson

Common Iterator Patterns begin end advance

Use std::begin, std::end, std::advance, and reverse iterators effectively.

begin() and end()

Every standard container exposes begin() (first element) and end() (one past the last). The half-open range [begin, end) is the canonical way to iterate.

std::vector<int> v = {1, 2, 3};
for (auto it = v.begin(); it != v.end(); ++it) {
    std::cout << *it << " ";
}

std::begin and std::end

Free functions in <iterator> that work on any container — including C arrays.

int arr[] = {1, 2, 3, 4};
for (auto it = std::begin(arr); it != std::end(arr); ++it) {
    std::cout << *it << " ";
}

All lessons in this course

  1. Iterator Categories input forward bidirectional random
  2. Common Iterator Patterns begin end advance
  3. C++20 Ranges Library Introduction
  4. Range Adaptors views::filter transform take
← Back to C++ Academy