In this blog post we are going to learn difference between std::advance and c++11 std::next. std::advance and std::next are used to advance the iterator by a certain position. Let us see below example of std::advance

Let us use std::next in place of advance in above example. See below code.

After looking the above two sample code , we can identify differences as

  • The std::advance returns nothing while std::next returns iterator.
  • The std::next is available in c++11 or c++17. so to compile c++ code in which std::next is used require -std=c++11 switch in g++ compiler.
  • std::advance modifies its argument while std::next  do not.
  • std::next will advance the iterator position by one by default while std::advance do not. so below program will print 1, because when there is no argument supplied to std::next, it will even advances position of iterator by one.

  • std::next() takes negative numbers just like std::advance, and in that case of negative number, it requires bidirectional iterator.


Related Contents to follow