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.

  

  

posted on 2013-01-22 16:15  androidme  阅读(143)  评论(0编辑  收藏  举报

导航