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 array::at() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par 2 min read string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn 1 min read std::move_backward in C++ Moves the elements in the range [first,last] starting from the end into the range terminating at result. The function begins by moving *(last-1) into *(result-1), and then follows backward by the elements preceding these, until first is reached (and including it). Template : BidirectionalIterator2 m 2 min read std::forward in C++ In C++, std::forward() is a template function used for achieving perfect forwarding of arguments to functions so that it's lvalue or rvalue is preserved. It basically forwards the argument while preserving the value type of it.std::forward() was introduced in C++ 11 as the part of <utility> he 2 min read Vector back() in C++ STL In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly.Letâs take a quick look at a simple example that illustrates the vector back() method:C++#include <bits/stdc+ 2 min read Like