Need iterator when using ranged-based for loops

Use the old for loop as:

for (auto it = values.begin(); it != values.end();  ++it )
{
       auto & value = *it;
       //...
}

With this, you’ve value as well as iterator it. Use whatever you want to use.


EDIT:

Although I wouldn’t recommended this, but if you want to use range-based for loop (yeah, For whatever reason :D), then you can do this:

 auto it = std::begin(values); //std::begin is a free function in C++11
 for (auto& value : values)
 {
     //Use value or it - whatever you need!
     //...
     ++it; //at the end OR make sure you do this in each iteration
 }

This approach avoids searching given value, since value and it are always in sync.

Leave a Comment