Best way to get the index of an iterator
http://stackoverflow.com/questions/2152986/best-way-to-get-the-index-of-an-iterator
it - vec.begin()
std::distance(vec.begin(), it)
it - vec.begin()
takes constant time, but the operator -
is only defined on random access iterators, so the code won't compile at all with list iterators, for example.
std::distance(vec.begin(), it)
works for all iterator types, but will only be a constant-time operation if used on random access iterators.