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

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

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