Iterators
An iterator is an object that points at an element in a sequence and knows how to move to the next one. Containers expose begin and end. Algorithms read and write through iterators so one std::find works on a vector, a list, and a raw array.
Detailed Explanation
A pointer into an array is the original iterator: *p is the element, ++p moves, p == end stops. The STL generalises that idea. v.begin() is an iterator to the first element. v.end() is a past-the-end iterator: it is not dereferenceable, and it is the sentinel every algorithm compares against.
The pair [begin, end) is a half-open range. It contains begin and does not contain end. An empty range has begin == end. This convention makes end - begin equal the size for random-access iterators and makes empty ranges need no special case.
*it yields a reference to the element (or a proxy, for vector<bool>). ++it advances. it == jt asks whether they point at the same position. Everything else — --it, it + n, it->m — depends on the iterator category.
You obtain iterators from containers (begin, end, rbegin, rend, cbegin, cend), from C arrays via pointer arithmetic, and from streams (std::istream_iterator). C++11 range-for uses them under the hood. C++20 ranges still use iterators; they add sentinels that may be a different type from the iterator.
Invalidation is the hard part. A vector reallocation invalidates every iterator. A map insert invalidates nothing. An algorithm that takes v.end() computed before a push_back is already wrong. The container's page documents the rules; treat them as part of the type.
const_iterator reads. iterator writes. cbegin() always returns a const iterator even on a mutable container, which is what you want when passing a range to a read-only algorithm.
A past-the-end iterator may not be decremented unless the container says so (bidirectional iterators can -- from end() to the last element). Dereferencing end() is undefined behaviour. Dereferencing an invalidated iterator is undefined behaviour. Comparing iterators from two different containers is undefined behaviour.
Raw pointers are iterators. A function that takes T* first, T* last is an iterator API. std::vector<T>::iterator may be T* on your library. Do not assume it is not; do not assume it is.
Prefer algorithms plus iterators over a handwritten loop when the algorithm names the intent (find, copy, sort). Write a loop when the body is the interesting part.
Code Examples
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{4, 8, 15, 16};
auto it = std::find(v.begin(), v.end(), 15);
if (it != v.end()) {
std::cout << *it << '\n'; // prints: 15
*it = 0;
}
std::cout << v[2] << '\n'; // prints: 0
}#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> names{"ada", "al"};
for (auto it = names.begin(); it != names.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n'; // prints: ada al
}Common Gotchas
Dereferencing `end()`
`*v.end()` is undefined, even if you think there is a sentinel value there.
Using an iterator after a vector reallocation
`auto it = v.begin(); v.push_back(1); *it;` is undefined if `push_back` grew the buffer.
Erase-while-iterate
`it = c.erase(it);` reseats. `c.erase(it++);` is the list/map form. Mixing them up is a use-after-free of the iterator.
`end()` is not unique across containers
do not store one container's `end` and compare it to another container's iterator.