Welcome to the world of iterators in C++! Iterators are fundamental to the C++ Standard Library, enabling efficient traversal and manipulation of container elements. The goal of this lesson is to help you understand what iterators are, how to use them effectively, and why they are essential.
By the end of this lesson, you'll know how to declare and use iterators to traverse and modify elements in a std::vector
.
So, what exactly is an iterator? An iterator is an object that lets you access elements in a container (such as std::vector
, std::list
, etc.) sequentially without exposing the container's underlying representation.
There are several types of iterators in C++:
For this lesson, we will focus on Random-Access Iterators, typically used with std::vector
.
To iterate over the elements of a vector, we use the begin()
and end()
member functions:
An iterator can be incremented, decremented, and dereferenced. Let's break this down:
Remember, the dereference operator (*
) is used to obtain the value that the iterator points to, much like how you dereference pointers.
C++1#include <iostream> 2#include <vector> 3 4int main() { 5 std::vector<int> numbers = {1, 2, 3, 4, 5}; 6 std::vector<int>::iterator it; 7 8 // Incrementing and dereferencing the iterator 9 it = numbers.begin(); 10 std::cout << *it << std::endl; // Output: 1 11 12 ++it; 13 std::cout << *it << std::endl; // Output: 2 14 15 return 0; 16}
In this snippet, we initialize it
to the beginning of the vector and increment it to access the second element. Note that:
<data type>::iterator
*
, like a pointer.Now, let's use an iterator to traverse the entire std::vector
and print each element:
C++1#include <iostream> 2#include <vector> 3 4int main() { 5 std::vector<int> numbers = {1, 2, 3, 4, 5}; 6 std::vector<int>::iterator it; 7 8 std::cout << "Elements in the vector: "; 9 for (it = numbers.begin(); it != numbers.end(); ++it) { 10 std::cout << *it << " "; 11 } 12 std::cout << std::endl; // Output: Elements in the vector: 1 2 3 4 5 13 14 return 0; 15}
In this code, we use a for
loop with the iterator to traverse the vector from begin()
to end()
, printing each element.
numbers.begin()
, which is the first element;numbers.end()
.Let's take it a step further by modifying the elements in the vector using an iterator:
C++1#include <iostream> 2#include <vector> 3 4int main() { 5 std::vector<int> numbers = {1, 2, 3, 4, 5}; 6 std::vector<int>::iterator it; 7 8 // Modifying elements through the iterator 9 for (it = numbers.begin(); it != numbers.end(); ++it) { 10 *it *= 2; // Doubling each element 11 } 12 13 // Displaying the modified vector elements 14 std::cout << "Modified elements in the vector: "; 15 for (it = numbers.begin(); it != numbers.end(); ++it) { 16 std::cout << *it << " "; 17 } 18 std::cout << std::endl; // Output: Modified elements in the vector: 2 4 6 8 10 19 20 return 0; 21}
In this example, we traverse the vector with the iterator, multiply each element by 2, and then print the modified elements.
In addition to forward iterators, C++ provides reverse iterators, which allow traversal of the container in reverse order. rbegin()
and rend()
are member functions that return reverse iterators.
Here's an example of how to use reverse iterators:
C++1#include <iostream> 2#include <vector> 3 4int main() { 5 std::vector<int> numbers = {1, 2, 3, 4, 5}; 6 std::vector<int>::reverse_iterator rit; 7 8 std::cout << "Elements in reverse order: "; 9 for (rit = numbers.rbegin(); rit != numbers.rend(); ++rit) { 10 std::cout << *rit << " "; 11 } 12 std::cout << std::endl; // Output: Elements in reverse order: 5 4 3 2 1 13 14 return 0; 15}
In this code, we use a reverse iterator to traverse the vector from the last element to just before the first element, printing each element in reverse order.
begin()
and end()
mark the start and end of container traversal, while rbegin()
and rend()
do the same for reverse traversal.Now that you've learned about iterators, it's time to get hands-on! You'll practice creating vectors, using iterators to traverse them, and modifying elements using iterators. This practice will solidify your understanding and prepare you for more advanced topics. Happy coding!