C++ Iterators
An iterator is a value that marks a position inside a container and can move to the next element. This lesson names iterator types directly and walks containers forward and backward.
Walking with an iterator type
The iterator starts at begin and steps with ++ until it reaches end. Each step prints the element at its position.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << "\n";
}
return 0;
}Iterator arithmetic
Adding 2 to the begin position lands on the third element. Subtracting begin from end gives the distance between them as a count of elements.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30, 40};
std::vector<int>::iterator start = numbers.begin();
std::cout << "Third: " << *(start + 2) << "\n";
std::cout << "Distance: " << numbers.end() - numbers.begin() << "\n";
return 0;
}Walking in reverse
rbegin starts at the last element and rend marks the end of the reverse walk. The same ++ step now moves toward the front of the vector.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
for (auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
std::cout << *it << "\n";
}
return 0;
}A const_iterator reads
A const_iterator starts at begin and reads the element at its position. One step moves it to the second element, and it reads that element too.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
std::vector<int>::const_iterator it = numbers.begin();
std::cout << *it << "\n";
++it;
std::cout << *it << "\n";
return 0;
}A const_iterator cannot write
This program tries to store a new value through a const_iterator. The compiler refuses the program and reports the error line below.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
std::vector<int>::const_iterator it = numbers.begin();
*it = 99;
std::cout << *it << "\n";
return 0;
}Example
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> ages;
ages["Ana"] = 25;
ages["Bo"] = 28;
ages["Hana"] = 31;
for (std::map<std::string, int>::iterator it = ages.begin(); it != ages.end(); ++it) {
std::cout << it->first << " " << it->second << "\n";
}
return 0;
}Key points
- Dereferencing an iterator with * reads the element at its position.
- The ++ step moves an iterator to the next element.
- begin gives the position of the first element, and end gives the position after the last element.
- Adding a number to an iterator moves it forward by that many elements.
- rbegin and rend give positions that walk a container in reverse order.
- A const_iterator can read the element at its position but cannot change it.
In this example, an iterator of the map type walks the map from begin to end. Each position gives a pair, so the print reads the key and the value at that position. The pairs print in sorted key order, Ana first and Hana last.
Note
A vector that grows can move its storage, so a position taken before the growth must not be used after it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does end() mark for a container?