Iterators in C++
Introduction
A common mistake when using for loops is trying to access an element of the array that is outside the bounds of the array. For example, when trying to access the last element of an array with 5 elements, a programmer may type arr[5]
, thinking that they are accessing the 5th element of the array, when in fact they are accessing the 6th. This is because arrays are zero-indexed, meaning that the first element of the array is always indexed with 0, and the last is always the array size - 1, or in this case, 4.
Here is an example as to how we might loop over an array without iterators via an index variable.
#include <array>
#include <iostream>
#include <iterator>
int main(int argc, char** argv) {
std::array<int, 3> a {3, 6, 9};
for (int i = 0; i < 3; i++) {
std::cout << a[i] << std::endl;
}
std::cout << std::endl;
return 0;
}
The value for the position where the array ends has to be defined by the programmer in the above example, which is correct, but the programmer may make a mistake. An alternative is to use a range based for loop as shown below:
#include <array>
#include <iostream>
#include <iterator>
int main(int argc, char** argv) {
std::array<int, 3> a {3, 6, 9};
for (int i : a) {
std::cout << i << std::endl;
}
std::cout << std::endl;
return 0;
}
By using a range based for loop, we can avoid this problem. int i
is the value that will be used to access elements of the array, and a
on the right side of the colon is the array to loop over. It is common for this value to be marked with the auto keyword, which allows for automatic type deduction, meaning that the compiler can work out the type of i based on the type of std::array, which is int.
Iterators are a way of looping over the elements in an array without the risk of accessing data outside of the array. Collections in C++ can be iterated over, and in this example, I am iterating over std::array
.
#include <array>
#include <iostream>
#include <iterator>
int main(int argc, char** argv) {
std::array<int, 3> a {3, 6, 9};
for (std::array<int, 3>::iterator it = a.begin(); it < a.end(); it++) {
std::cout << *it << ' ';
}
std::cout << std::endl;
return 0;
}
In order to use iterators, we need to get access to the iterator library by a using directive. This code iterates over every element of the array and prints its value to standard output.
This gives the expected result:
3 6 9
Iterators are just an abstraction of pointers.
More content goes here