C++ Iterators
In this page:
for (auto it = container.begin(); it != container.end(); ++it) {
*it; // current element
}
for (auto it = container.rbegin(); it != container.rend(); ++it) { } // reverse
Introduction to Iterators
Iterators are objects that behave like generalized pointers into a container's elements, giving you a uniform way to step through and access elements sequentially regardless of how that particular container is implemented internally.
Example: Introduction to Iterators
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
std::vector<int>::iterator it = nums.begin();
// Print to the console with cout
std::cout << *it << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reverse Iterators
Reverse iterators let you traverse a container from its last element back to its first; incrementing a reverse iterator actually moves it backward through the underlying container, which is exactly the behavior algorithms like std::sort(rbegin(), rend()) rely on.
Example: Reverse Iterators
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Loop: repeat while the condition holds
for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
// Print to the console with cout
std::cout << *it << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Const Iterators
Const iterators provide read-only access to a container's elements, letting you traverse and inspect data while the compiler guarantees you can't accidentally modify any of the underlying values through that iterator.
Example: Const Iterators
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {1, 2, 3};
for (auto it = nums.cbegin(); it != nums.cend(); ++it) {
std::cout << *it << " ";
// *it = 5; would fail to compile: read-only
}
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterator Arithmetic and Operations
Random-access containers like vector support arithmetic directly on their iterators — you can compute the distance between two iterators or advance one by an arbitrary number of steps in constant time, unlike simpler forward-only iterators.
Example: Iterator Arithmetic and Operations
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {10, 20, 30, 40};
// Declare it and set it to nums.begin()
auto it = nums.begin();
it += 2;
// Print to the console with cout
std::cout << *it << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Inserter Iterators
Inserter iterators, such as those produced by std::back_inserter, insert new elements into a container rather than overwriting existing ones at that position, which is what lets algorithms like std::copy safely grow a destination container as they run.
Example: Inserter Iterators
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>
// Program execution starts in main()
int main() {
// Create a vector named source (a resizable array)
std::vector<int> source = {1, 2, 3};
// Create a vector named destination (a resizable array)
std::vector<int> destination;
std::copy(source.begin(), source.end(), std::back_inserter(destination));
for (int n : destination) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dereferencing
end(), which points one past the last element and is invalid. - Using an iterator after the container was modified and invalidated it.
- Mixing iterators from two different containers in one range.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: