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.

  

  

原文地址:https://www.cnblogs.com/androidme/p/2871585.html