Can I increment an iterator by just adding a number?

It works if the iterator is a random access iterator, which vector’s iterators are (see reference). The STL function std::advance can be used to advance a generic iterator, but since it doesn’t return the iterator, I tend use + if available because it looks cleaner.

C++11 note

Now there is std::next and std::prev, which do return the iterator, so if you are working in template land you can use them to advance a generic iterator and still have clean code.

Leave a Comment