I found myself writing the following a lot:
int location =2;
vector vec;
vector::iterator it=vec.begin();
/..../
std::advance(it, location
That depends on what you need:
If you need genericity, use std::advance(it,2)
. If someone comes along and changes your std::vector
into a std::list
, the code will still compile, even though advancing now takes linear time instead of constant time.
If you need performance, use it+=2
. If someone comes along and changes your std::vector
into a std::list
, the code will fail to compile, pointing (maybe with a helpful comment) at a serious performance issue.