C++ Reference ยท Containers
C++ iterators
Reference for using C++ iterators: begin and end, half-open ranges, const and reverse iterators, std::next, advance, distance, insert iterators and stream iterators.
An iterator is a small object that refers to one element of a sequence and can move to the next. It generalizes the pointer: *it reads the element, ++it advances, and it == end tells you the traversal is over. Every standard container provides iterators, and every standard algorithm takes them, which is what lets one std::find or std::sort work for arrays, vectors, lists and more.
Ranges are written as a pair [begin, end): begin() refers to the first element and end() to the position one past the last. The end iterator is never dereferenced. An empty range has begin() == end().
for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << ' '; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Obtaining and using iterators
| Expression | Description |
|---|---|
c.begin(), c.end() |
Iterators to the first element and one past the last. |
c.cbegin(), c.cend() |
Same, but const_iterator: elements cannot be modified. |
c.rbegin(), c.rend() |
Reverse iterators that walk from the last element to the first. |
std::begin(x), std::end(x) |
Work on containers, built-in arrays and initializer lists. |
std::next(it, n), std::prev(it, n) |
Return a moved copy without changing it (n defaults to 1). |
std::advance(it, n) |
Moves it by n steps in place (negative n needs bidirectional iterators). |
std::distance(a, b) |
Number of steps from a to b. |
std::back_inserter(c), front_inserter(c), inserter(c, pos) |
Output iterators that call push_back, push_front or insert. |
std::istream_iterator<T>, std::ostream_iterator<T> |
Read values from or write values to a stream. |
std::make_move_iterator(it) |
Dereferences to an rvalue, so algorithms move instead of copy. |
Errors / undefined behavior: dereferencing end(), advancing past end(), or using an iterator after it was invalidated is undefined behavior. Comparing iterators from different containers is also undefined.
Usage notes
- Prefer range-based
for(for (auto& x : v)) when you do not need the position; use iterators for erasing during traversal, for algorithms and for partial ranges. std::nextis safe in expressions:v.begin() + 3works for random access only,std::next(v.begin(), 3)for every category.- Erase inside a loop:
erasereturns the iterator to the next element; writeit = c.erase(it)and increment only in the other branch. - Reverse iterators and
base():rit.base()is the forward iterator one position after the elementritrefers to. const_iteratorconversions: aniteratorconverts toconst_iteratorimplicitly, never the other way.- Iterator range from the wrong container, or a begin that comes after the end, is a common source of crashes. Pass both from the same object.
- Output iterators need room:
std::copyinto avectorwithout space writes past its end. Useback_inserteror size the destination first. - C++20 ranges let you pass the container itself (
std::ranges::sort(v)) and use sentinels; see Constrained algorithms.