Iterating over std::vector
suggest changeYou can iterate over a std::vector in several ways. For each of the following sections, v is defined as follows:
std::vector<int> v;
Iterating in the Forward Direction
// Range based for
for(const auto& value: v) {
std::cout << value << "\n";
}
// Using a for loop with iterator
for(auto it = std::begin(v); it != std::end(v); ++it) {
std::cout << *it << "\n";
}
// Using for_each algorithm, using a function or functor:
void fun(int const& value) {
std::cout << value << "\n";
}
std::for_each(std::begin(v), std::end(v), fun);
// Using for_each algorithm. Using a lambda:
std::for_each(std::begin(v), std::end(v), [](int const& value) {
std::cout << value << "\n";
});
// Using a for loop with iterator
for (std::vector<int>::iterator it = std::begin(v); it != std::end(v); ++it) {
std::cout << *it << "\n";
}
// Using a for loop with index
for (std::size_t i = 0; i < v.size(); ++i) {
std::cout << v[i] << "\n";
}
Iterating in the Reverse Direction
#include <iostream>
#include <ranges>
#include <vector>
int main() {
const std::vector values{1, 2, 3, 4};
for (int value : values | std::views::reverse) {
std::cout << value << '\n';
}
}
C++20's std::views::reverse provides a direct range-based solution. With earlier language versions, use reverse iterators:
std::for_each(std::rbegin(v), std::rend(v), [](const auto& value) {
std::cout << value << '\n';
});
for (auto it = std::rbegin(v); it != std::rend(v); ++it) {
std::cout << *it << '\n';
}
// Using a for loop with index
for(std::size_t i = 0; i < v.size(); ++i) {
std::cout << v[v.size() - 1 - i] << "\n";
}
Enforcing const elements
Since C++11 the cbegin() and cend() methods allow you to obtain a constant iterator for a vector, even if the vector is non-const. A constant iterator allows you to read but not modify the contents of the vector which is useful to enforce const correctness:
// forward iteration
for (auto pos = v.cbegin(); pos != v.cend(); ++pos) {
// type of pos is vector<T>::const_iterator
// *pos = 5; // Compile error - can't write via const iterator
}
// reverse iteration
for (auto pos = v.crbegin(); pos != v.crend(); ++pos) {
// type of pos is vector<T>::const_reverse_iterator
// *pos = 5; // Compile error - can't write via const iterator
}
// The function object is called with T&.
std::for_each(v.begin(), v.end(), Functor());
// The function object is called with const T&.
std::for_each(v.cbegin(), v.cend(), Functor());
std::as_const (C++17) extends this to range iteration:
for (auto const& e : std::as_const(v)) {
std::cout << e << '\n';
}
This is easy to implement in earlier versions of C++:
template <class T>
constexpr typename std::add_const<T>::type& as_const(T& t) noexcept {
return t;
}
A Note on Efficiency
std::vector manages a dynamically allocated contiguous array. Row-major order is about multidimensional arrays; a vector is one contiguous sequence, so walking it in index order is the cache-friendly direction. The difference between iterating a std::vector and a raw array is negligible, as discussed here and here.