Reputation: 51445
I was playing with range loops, eg:
for (auto i : range) {
which is equivalent to
auto it = range.begin();
auto i = *it;
but what i want is this:
for (auto it : range) {
*it; // the it var is not derefernced by range loop
is there a way to accomplish this? maybe something like:
for (auto it : make_range(range)) {
*it; // the it var is not derefernced by range loop
....
One solution I found so far is using boost::irange, eg:
for (auto it : irange(range.begin(), range.end())) {
Upvotes: 3
Views: 334
Reputation: 595652
The whole purpose of a for-range loop is to iterate through the contents of a container. So no, there is no native means to gain access to the iterator used by the loop. If you need that, you should not be using a for-range loop to begin with. The only option to gain access to a container iterator in a for-range loop is to write a custom adapter class that wraps the container and exposes its own iterators that return container iterators when dereferenced. Then you iterate through the adapter instead of the container directly.
Upvotes: 4