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
Post a Comment