An iterator is an object that points to an element in a container. As the name suggests, iterators are used to iterate through a container. Iterators can be used like pointers: * accesses the element and ++ moves to the next element:
std::vector#include <vector><int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
std::vector#include <vector><int>::iterator it = v.begin();
*it; // 10
++it;
*it; // 20begin() returns the iterator to the first element and end() returns the iterator one past the last element, so the range is [begin, end):
v.end() - v.begin(); // 3
*v.end(); // Undefined behaviour because end() doesn't point to any element.Iterators can be compared with other iterators from the same container. Comparing begin() with end() checks if a container is empty:
std::vector#include <vector><int> u;
u.begin() == u.end(); // trueIterators can be used in a loop with end() to know when all elements have been iterated over:
for (std::vector#include <vector><int>::iterator it = v.begin(); it != v.end(); ++it) {
*it;
}Const Iterator
A const_iterator can read but not modify the element it points to. The iterator itself can still move:
std::vector#include <vector><int>::const_iterator cit = v.begin();
*cit; // 10
*cit = 5; // ERROR.
++cit; // OK.Calling begin() and end() on a const container returns a const_iterator:
const std::vector#include <vector><int>& cv = v;
std::vector#include <vector><int>::const_iterator a = cv.begin(); // OK.
std::vector#include <vector><int>::iterator b = cv.begin(); // ERROR.Iterator Categories
Iterators are grouped into categories by supported operations:
| Category | *it | *it = x | ++ | Multi-pass | -- | +, -, [], < | Containers |
|---|---|---|---|---|---|---|---|
| Input | ✓ | ✓ | std::istream_iterator | ||||
| Forward | ✓ | ✓ | ✓ | ✓ | |||
| Bidirectional | ✓ | ✓ | ✓ | ✓ | ✓ | std::list, std::set#include <set>, std::map#include <map> | |
| Random access | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | std::vector#include <vector>, std::deque, std::string#include <string> |
| Output | ✓ | ✓ | std::ostream_iterator, std::back_inserter |
Note
std::set#include <set> and std::map#include <map> use bidirectional iterators, but set elements and map keys do not support writing.
Using an operation that the iterator's category doesn't support is a compile error:
std::set#include <set><int> s;
std::set#include <set><int>::iterator st = s.begin();
st += 2; // ERROR.
++st; // OK.Multi-pass means multiple iterators can pass over the same range. Stream iterators aren't multi-pass because moving the iterator consumes the input:
std::istringstream#include <sstream> ss("1 2 3");
std::istream_iterator<int> it1(ss);
std::istream_iterator<int> it2 = it1;
++it1;
++it2;
*it1; // 2
*it2; // 3Invalidation
Iterators become invalidated when the element they point to is moved or removed. Using an invalidated iterator is undefined behaviour:
std::vector#include <vector><int>::iterator it = v.begin();
v.push_back(40); // May reallocate.
*it; // Undefined behaviour if the vector reallocated.cbegin() and cend() always return a const_iterator, even on a non-const container:
std::vector#include <vector><int>::const_iterator cit = v.cbegin();
*cit = 5; // ERROR.std::next() and std::prev() return a new iterator moved forward or backward by a number of steps. They work with any category that can move in that direction:
std::set#include <set><int> s = {1, 2, 3, 4};
std::set#include <set><int>::iterator it = std::next(s.begin(), 2);
*it; // 3
*std::prev(it); // 2std::begin#include <iterator>() and std::end#include <iterator>() return iterators for containers and C-style arrays:
int arr[] = {1, 2, 3};
int* first = std::begin#include <iterator>(arr); // Points to 1.
int* last = std::end#include <iterator>(arr); // Points one past 3.
Dan