Iterating Vectors: index range-for iterators
Iterate a vector with index access, range-for, and explicit iterators.
Iterating Vectors: index range-for iterators 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.
Three Ways to Iterate
You can walk a vector with:
- Index-based
for (size_t i = 0; i < v.size(); ++i) - Range-based
for (auto& x : v) - Iterators
for (auto it = v.begin(); it != v.end(); ++it)
Index-Based Loop
Familiar from C. Useful when you need the index alongside the value.
for (size_t i = 0; i < v.size(); ++i) {
std::cout << i << ": " << v[i] << "\n";
}size_t vs int
size() returns size_t — unsigned. Mixing with a signed int counter causes compiler warnings and subtle bugs. Use size_t or C++20 std::ssize.
Range-Based for
The cleanest form when you do not need indices. Choose the right reference qualifier.
for (const auto& x : v) std::cout << x << " "; // read only
for (auto& x : v) x *= 2; // modify in placeIterators
An iterator is a generalized pointer. v.begin() points to the first element, v.end() one past the last.
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}const_iterator
Use cbegin() and cend() when you do not need to modify elements — the iterator type is const_iterator.
Reverse Iterators
rbegin() and rend() iterate from back to front. Dereferencing yields the same elements but in reverse order.
for (auto it = v.rbegin(); it != v.rend(); ++it) {
std::cout << *it << " ";
}Iterator Arithmetic
Vector iterators are random access: you can add and subtract integers and compare with <.
auto it = v.begin();
it += 3; // skip 3 elements
std::cout << *it;Algorithms Use Iterators
STL algorithms take iterator pairs. Pass v.begin(), v.end() to algorithms like std::sort or std::find.
#include <algorithm>
std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 42);When Iterators Get Invalidated
Adding to a vector may reallocate. All iterators, pointers, and references into the vector become invalid after any operation that changes capacity.
Pitfall: Modifying While Iterating
Never call push_back or erase during a range-based for over the same vector. Build a new vector or collect indices to act on after.
Quick Check
Which iteration form invalidates its iterator if push_back triggers a reallocation?
Recap
Pick range-based for when you do not need indices, index-based for when you do, and iterator-based for when working with algorithms. Reallocation invalidates iterators — never push_back while iterating.
Frequently asked questions
Is the “Iterating Vectors: index range-for iterators” lesson free?
Yes — the full text of “Iterating Vectors: index range-for iterators” 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 “Iterating Vectors: index range-for iterators”?
Iterate a vector with index access, range-for, and explicit iterators. 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 “Iterating Vectors: index range-for iterators” 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
- std::vector Basics: push_back size capacity
- Iterating Vectors: index range-for iterators
- Modifying Vectors: insert erase clear
- Vector vs std::array vs C Array