0Pricing
C++ Academy · Lesson

Range-Based for Loop with Containers

Iterate cleanly over containers with the C++11 range-based for syntax.

Range-Based for Loop with Containers is a free C++ Academy lesson on CoddyKit — lesson 3 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.

The Modern Loop

C++11 introduced the range-based for loop — iterate any container without explicit indices or iterators. Cleaner and harder to get wrong.

std::vector<int> v = {1, 2, 3, 4, 5};
for (int x : v) {
    std::cout << x << " ";
}

By Value

The example above copies each element into x. Fine for cheap types like int; expensive for large objects.

By const Reference

For non-trivial types, use const auto& to avoid copies and signal you will not modify the elements.

std::vector<std::string> names = {"Ada", "Bob"};
for (const auto& name : names) {
    std::cout << name << "\n";
}

By Reference for Mutation

Use a plain reference to modify elements in place.

std::vector<int> v = {1, 2, 3};
for (auto& x : v) {
    x *= 10;
}
// v is now {10, 20, 30}

auto for Type Deduction

Let the compiler deduce the element type. With strings, vectors of structs, and maps, this saves a lot of typing.

Iterating a Map

Each element is a std::pair<const Key, Value>. Use structured bindings (C++17) for cleaner code.

std::map<std::string, int> ages = {{"Ada",30},{"Bob",24}};
for (const auto& [name, age] : ages) {
    std::cout << name << ": " << age << "\n";
}

Iterating Strings

A std::string is a container of char.

std::string s = "hello";
for (char c : s) {
    std::cout << c << " ";
}

Initializer List Loops

You can range-loop over a brace-enclosed list directly.

for (int n : {1, 2, 4, 8, 16}) {
    std::cout << n << " ";
}

Reverse Range-Based Loop

Standard range-for goes forward. For reverse iteration use std::ranges::reverse_view (C++20) or manual reverse iterators.

#include <ranges>
for (auto x : v | std::views::reverse) {
    std::cout << x;
}

Limitations

Range-for cannot:

  • Know the current index without a separate counter
  • Skip elements or modify the container while iterating
  • Iterate two containers in parallel (use a regular for or zip)

Custom Types in Range-For

To make your own class work with range-for, provide begin() and end() members or free functions that return iterators.

Quick Check

Which form of range-based for loop modifies elements in the container?

Recap

Range-based for is the cleanest way to iterate. Use const auto& by default, auto& to modify, and structured bindings to destructure pairs and tuples.

Frequently asked questions

Is the “Range-Based for Loop with Containers” lesson free?

Yes — the full text of “Range-Based for Loop with Containers” 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 “Range-Based for Loop with Containers”?

Iterate cleanly over containers with the C++11 range-based for syntax. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Range-Based for Loop with Containers” 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. Branching: if-else, ternary, switch-case
  2. Loop Patterns: for, while, do-while
  3. Range-Based for Loop with Containers
  4. Early Returns break continue and goto
← Back to C++ Academy