std::advance in C++ Last Updated : 26 Apr, 2022 Comments Improve Suggest changes Like Article Like Report std::advance advances the iterator 'it' by n element positions. Syntax : template void advance (InputIterator& it, Distance n); it : Iterator to be advanced n : Number of element positions to advance. This shall only be negative for random-access and bidirectional iterators. Return type : None. Motivation problem : A vector container is given. Task is to print alternate elements. Examples : Input : 10 40 20 50 80 70 Output : 10 20 80 CPP // C++ program to illustrate // using std::advance #include <bits/stdc++.h> // Driver code int main() { // Vector container std::vector<int> vec; // Initialising vector for (int i = 0; i < 10; i++) vec.push_back(i * 10); // Printing the vector elements for (int i = 0; i < 10; i++) { std::cout << vec[i] << " "; } std::cout << std::endl; // Declaring the vector iterator std::vector<int>::iterator it = vec.begin(); // Printing alternate elements while (it < vec.end()) { std::cout << *it << " "; std::advance(it, 2); } } Output: 0 10 20 30 40 50 60 70 80 90 0 20 40 60 80 C++ #include <iostream> #include<bits/stdc++.h> using namespace std; int main() { vector<int >vect(10); //insert the ten element in the vector for(int i=1;i<=10;i++) { vect[i-1]=i; } //iterator pointing to first element. vector<int >::iterator it=vect.begin(); for(int i=1;i<=10;i++) { cout<<*it<<" "; it++; } vector<int >::iterator it1=vect.begin(); //here it is pointing to the 3rd element. advance(it1,2);//here second argument is the index base. cout<<endl; cout<<*it1; //print it1 pointing to the 3rd position. return 0; } /* This code is contributed by Sameer Hake/* Output1 2 3 4 5 6 7 8 9 10 3 Comment More infoAdvertise with us Next Article std::advance in C++ R Rohit Thapliyal , Sameer Hake Improve Article Tags : Misc C++ cpp-iterator STL Practice Tags : CPPMiscSTL Similar Reads 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 betwe 3 min read STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read std::distance in C++ The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples.Example:C++// C++ Program to illustrate the us 5 min read Advanced C++ Quizzes C++ provides many advanced features like preprocessors, multithreading, signal handling, and more. Understanding these concepts is helpful in writing high-performance code. This quiz will help you test your knowledge of advanced C++ topics.The below quizzes contain some questions each from the given 1 min read std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s 5 min read Like