std::next vs std::advance in C++

std::advance and std::next are used to advance the iterator by a certain position, such that we can make the iterator point to a desired position. Although both have same purpose, but their implementation is different from each other. This makes it important for us to understand the difference between the two.
In C++11, std::next() will advance by one by default, whereas std::advance() requires a distance.

  1. Syntactical Difference: Syntax of std::advance and std::next is:
    // Definition of std::advance()
    template
    void advance( InputIt& it, Distance n );
    
    it: Iterator to be advanced
    n: Distance to be advanced
    
    // Definition of std::next()
    ForwardIterator next (ForwardIterator it,
           typename iterator_traits::difference_type n = 1);
    
    it: Iterator pointing to base position
    n: Distance to be advanced from base position.
    
    • Return type: std::advance does not return anything, whereas std::next returns an iterator after advancing n positions from the given base position.
    • As in the syntax of std::next(), it will at least advance the iterator by one position, even if we do not specify the position which it has to advance as it has a default value one, whereas if we use std::advance, it has no such default argument.
  2. Working
    • Argument Modification: std::advance modifies it arguments such that it points to the desired position, whereas, std::next does not modify its argument, infact it returns a new iterator pointing to the desired position.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C++ program to demonstrate
      // std::advance vs std::next
      #include <iostream>
      #include <iterator>
      #include <deque>
      #include <algorithm>
      using namespace std;
      int main()
      {
          // Declaring first container
          deque<int> v1 = { 1, 2, 3 };
        
          // Declaring second container for
          // copying values
          deque<int> v2 = { 4, 5, 6 };
        
          deque<int>::iterator ii;
          ii = v1.begin();
          // ii points to 1 in v1
        
          deque<int>::iterator iii;
          iii = std::next(ii, 2);
          // ii not modified
        
          // For std::advance
          // std::advance(ii, 2)
          // ii modified and now points to 3
        
          // Using copy()
          std::copy(ii, iii, std::back_inserter(v2));
          // v2 now contains 4 5 6 1 2
        
          // Displaying v1 and v2
          cout << "v1 = ";
        
          int i;
          for (i = 0; i < 3; ++i) {
              cout << v1[i] << " ";
          }
        
          cout << "\nv2 = ";
          for (i = 0; i < 5; ++i) {
              cout << v2[i] << " ";
          }
        
          return 0;
      }

      chevron_right

      
      

      Output:

      v1 = 1 2 3
      v2 = 4 5 6 1 2 
      

      Explanation: As can be seen, we want to make ii point to 2 spaces ahead of where it is pointing, so if we use std::advance ii will be pointing to two spaces ahead, whereas if we use std::next, then ii will not be advanced, but an iterator pointing to the new position will be returned, and will be stored in iii.

  3. Pre-requisite: std::next requires the iterator passed as argument to be of type at least forward iterator, whereas std::advance doesnot have such restrictions, as it can work with any iterator, even with input iterator or better than it.

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.



Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.