std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (auto i : v)
{
// access by value, the type of i is int
std::cout << i << ' ';
}
std::cout << '\n';
I found this as part of another question
How to navigate through a vector using iterators? (C++)
I would like to know how the following works ?
for (auto i : v)
will navigate the entire vector, I need this information because I want to implement a custom vector class. In order to create custom vector class, do I need to create the begin(), end() functions and iterator member variable in it?
forreference have the equivalent "normal"forloops, if that's what you're wondering about. It will also tell you what is needed to support range-for in your classes.forloop works. For your iterator class, all you need is the!=(not equal),++it(prefix increment), and*(dereference) operators.