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

前端 未结 7 1232
萌比男神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:38

    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).

    0 讨论(0)
  • 2021-02-07 07:39

    It depends on the iterator. it=it+5 is faster if it's supported (it's only supported on random access iterators). If you want to advance a less-capable iterator (e.g. a forward iterator, or a bidirectional iterator), then you can use std::advance, but it's slower because it actually walks across all of the intermediate elements.

    0 讨论(0)
  • 2021-02-07 07:43

    std::advance works on non-random iterators too while the += version on works on random access sequences (vectors and the like).

    0 讨论(0)
  • 2021-02-07 07:44

    Adding will only work with random access iterators. std::advance will work with all sorts of iterators. As long as you're only dealing with iterators into vectors, it makes no real difference, but std::advance keeps your code more generic (e.g. you could substitute a list for the vector, and that part would still work).

    For those who care, the standard describes advance and distance as follows (§24.3.4/1):

    Since only random access iterators provide + and - operators, the library provides two function templates advance and distance. These function templates use + and - for random access iterators (and are, therefore, constant time for them); for input, forward and bidirectional iterators they use ++ to provide linear time implementations.

    Also note that starting with C++11, the standard added a parameter to std::next, so you can advance by a specified amount using it (and std::prev similarly). The difference from std::advance is that it returns the modified iterator (which std::advance doesn't), which can be convenient in some cases.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 07:58

    If you're never going to change the container (and you probably aren't), use + because it's easy to see and understand and leaves the code less cluttered.

    If you think you want to change the container, OR if you are working inside a template that might be instantiated on various container types, use advance because it works with anything.

    As a general rule, I don't worry about changing container types because I've found that when I do have to change a container type, I end up revisiting everywhere that container is used anyway, just to be sure I'm not doing anything that's suddenly stupid (like randomly plucking elements out of the middle of a list).

    0 讨论(0)
提交回复
热议问题