How to increment an iterator by 2?

Cute picture Cute · Jun 29, 2009 · Viewed 97.2k times · Source

Can anybody tell me how to increment the iterator by 2?

iter++ is available - do I have to do iter+2? How can I achieve this?

Answer

CB Bailey picture CB Bailey · Jun 29, 2009

std::advance( iter, 2 );

This method will work for iterators that are not random-access iterators but it can still be specialized by the implementation to be no less efficient than iter += 2 when used with random-access iterators.