I found myself writing the following a lot:
int location =2;
vector vec;
vector::iterator it=vec.begin();
/..../
std::advance(it, location
std::adnvance
is generic - it is useful if you don't always know type of underlying container - it works in all cases.
Yet it is efficient: std::advance
will do an optimisation if it passed an RandomAccessIterator (like one from std::vector
) and will increase iterator in loop for ForwardAccessIterator (as like one in std::list
).