C++ iterator::advance() Function



The C++ iterator::advance() function is used to increase iterator's current position . It accepts a single integer as an argument. The pointer is advanced to that integer position by the advance() method. The function only employs operator+ or operator- once if it is a random-access iterator. If not, the function repeatedly applies the increase or decrease operator until n items have been advanced.

Iterators operate as a bridge that connects algorithms to STL containers and allows the modification of the data present inside the container. To get the desired outcome, you can use them to iterate over the container, access and assign the values, and run different operators over them.

Syntax

Following is the syntax for C++ iterator::advance() Function −

void advance(InputIterator& it, Distance n);

Parameters

  • it − iterator that to be incremented and must statisfy the requirement of input iterator.
  • n − indicates the number of increments of the iterator is to be advanced.

Example 1

Let's consider the following example, where we are going to insert the elements in the vector and making the iterator pointing to the first element.

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main() {
   vector<int >vector1(5);
   for(int i=1; i<=5; i++) {
      vector1[i-1]=i;
   }
   vector<int >::iterator it=vector1.begin();
   for(int i=1; i<=5; i++) {
      cout<<*it<<" ";
      it++;
   }
   vector<int >::iterator it1=vector1.begin();
   advance(it1,3);
   cout<<endl;
   cout<<*it1;
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

1 2 3 4 5 
4  

Example 2

Consider the following example, where we are going to use advance() function and retrieving the output.

#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main () {
   list<int> list1;
   for (int i=0; i<10; i++)
      list1.push_back (i*10);
   list<int>::iterator it = list1.begin();
   advance (it,6);
   cout << "The Result is: " << *it << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

The Result is: 60

Example 3

In the following example, we are going to declare the vector and then applied with advance() function.

#include <iostream>
#include <iterator>
#include <vector>
int main() {
   std::vector<int> v{1,3,7,9,11};
   auto vi = v.begin();
   std::advance(vi, 3);
   std::cout << *vi << ' ';
}

Output

On running the above program, it will produce the following result −

9

Example 4

Following is the example where we are going to print the start of the iterator later, we increment it by using the advance() function and retrieve the output.

#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
int main() {
   vector<int> tutorial = {2,4,6,8,10};
   vector<int>::iterator itr;
   itr = tutorial.begin();
   cout << "The Start of iterator: ";
   cout << *itr << " ";
   cout << "\n\n";
   advance(itr,4);
   cout << "The Position of iterator now: ";
   cout << *itr << " ";
   cout << "\n\n";
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

The Start of iterator: 2 
The Position of iterator now: 10
Advertisements