0Pricing
C++ Academy · Lesson

Common Iterator Patterns begin end advance

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

Common Iterator Patterns begin end advance is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 << " ";
}

cbegin and cend

The c versions return const_iterator. Use when you do not need to modify and want the compiler to enforce it.

rbegin and rend

Reverse iterators. rbegin() points to the last element, rend() one before the first.

for (auto it = v.rbegin(); it != v.rend(); ++it) {
    std::cout << *it << " ";
}

std::next and std::prev

Return an iterator offset from another, without modifying the original.

auto it = v.begin();
auto next3 = std::next(it, 3);     // it + 3
auto prev1 = std::prev(it);         // it - 1

std::advance

Mutate an iterator in place by a distance. Selects the best implementation based on category.

auto it = v.begin();
std::advance(it, 5);          // moves it forward by 5

std::distance

Measure the number of steps between two iterators. O(1) for random access, O(n) otherwise.

auto d = std::distance(v.begin(), it);   // index of it

Insert Iterators

Adapters that turn assignment into insertion:

  • std::back_inserter — push_back
  • std::front_inserter — push_front
  • std::inserter — insert at a position
std::vector<int> dst;
std::copy(src.begin(), src.end(), std::back_inserter(dst));

Stream Iterators

Use streams like ranges with std::istream_iterator and std::ostream_iterator.

// Read all ints from cin, print to cout
std::copy(
    std::istream_iterator<int>(std::cin),
    std::istream_iterator<int>{},
    std::ostream_iterator<int>(std::cout, " ")
);

Erasing While Iterating

Erase returns the next valid iterator. Capture and reuse it.

for (auto it = v.begin(); it != v.end(); ) {
    if (*it < 0) it = v.erase(it);
    else         ++it;
}

Iterator Invalidation

Modifying a container can invalidate its iterators. Vectors invalidate on resize; lists keep iterators stable except for the erased ones.

Quick Check

Which function returns an iterator pointing to the position past the last element of a container?

Recap

Use begin()/end() for forward iteration, rbegin()/rend() for reverse. std::next, std::prev, std::advance, and std::distance work generically. Insert iterators adapt assignment into insertion.

Frequently asked questions

Is the “Common Iterator Patterns begin end advance” lesson free?

Yes — the full text of “Common Iterator Patterns begin end advance” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Common Iterator Patterns begin end advance”?

Use std::begin, std::end, std::advance, and reverse iterators effectively. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Common Iterator Patterns begin end advance” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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