Reputation: 11483
For example, if I use std::find_if
like this:
std::vector<Rect>::iterator it =
std::find_if(regions.begin(), regions.end(), find_region(x,y));
Can I get the index of the iterator it
in a straightforward way?
Upvotes: 2
Views: 210
Reputation: 4044
Vector iterators are random access, so you can simply use
it - regions.begin()
but std::distance works on a wider variety of iterator types.
Upvotes: 1
Reputation: 111
Use std::distance(). Ensure you don't accidentally invalidate the iterator by modifying the vector in between, though.
Upvotes: 1
Reputation: 40272
Try std::distance(regions.begin(), it)
from the <iterator>
header file.
Upvotes: 8