c++ - What is the most effective way to get the index of an iterator of an std::vector? -


i'm iterating on vector , need index iterator pointing at. afaik can done in 2 ways:

  • it - vec.begin()
  • std::distance(vec.begin(), it)

what pros , cons of these methods?

i prefer it - vec.begin() precisely opposite reason given naveen: wouldn't compile if change vector list. if during every iteration, end turning o(n) algorithm o(n^2) algorithm.

another option, if don't jump around in container during iteration, keep index second loop counter.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -