Advance iterator for the std::vector std::advance VS operator +?

前端 未结 7 1172
萌比男神i
萌比男神i 2021-02-07 07:36

I found myself writing the following a lot:

int location =2;
vector vec;
vector::iterator it=vec.begin();

/..../
std::advance(it, location         


        
7条回答
  •  有刺的猬
    2021-02-07 07:46

    Use std::advance. It is just as efficient (it uses iterator traits to just do iterator addition for random access iterators), and is more general in that it works on other kinds of iterators as well.

提交回复
热议问题