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   androidme  阅读(143)  评论(0编辑  收藏  举报

导航

< 2013年1月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示