C++ Academy · Lesson

Common Iterator Patterns begin end advance

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

Lesson 2 of 413 steps

Common Iterator Patterns begin end advance is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “Common Iterator Patterns begin end advance”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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