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
- Iterator Categories input forward bidirectional random
- Common Iterator Patterns begin end advance
- C++20 Ranges Library Introduction
- Range Adaptors views::filter transform take