Archived
1
Fork 0
This repository has been archived on 2025-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
it-works/iterators
Joshua Moerman (@Kassalade) 093e50fa3f markdown jeej
2011-05-22 17:54:40 +02:00
..
counting_iterator.hpp first draft of counting range-based-for-loops 2011-05-22 17:13:03 +02:00
main.cpp first draft of counting range-based-for-loops 2011-05-22 17:13:03 +02:00
README.md markdown jeej 2011-05-22 17:54:40 +02:00

counted

for(auto x : counted(v)) {
	... x.value ... x.index ...
}

x.value is a reference to the element in the container (so you can change it's value), x.index is what it is. Container should have forward iterators.

This headers is purely a handy tool for the range-based-for-loops (ie for(auto x : counter(v))). Using it explicitly with iterators is not recommended!

There is no const version of it. Doing for(const auto x : counted(v)) doesn't make it impossible to change x (so the element in the container can be modified).

Example:

for(auto x : counted(v)) {
	std::cout << "v[" << x.index << "] = " << x.value << std::endl;
	x.value *= 2;
}

This will output the container v, with it's indeces. And it will multiply every value in v by 2.