std::next in C++ Last Updated : 02 Aug, 2017 Summarize Comments Improve Suggest changes Share Like Article Like Report std::next returns an iterator pointing to the element after being advanced by certain no. of positions. It is defined inside the header file . It does not modify its arguments and returns a copy of the argument advanced by the specified amount. If it is a random-access iterator, the function uses just once operator + or operator - for advancing. Otherwise, the function uses repeatedly the increase or decrease operator (operator ++ or operator --) on the copied iterator until n elements have been advanced. Syntax: ForwardIterator next (ForwardIterator it, typename iterator_traits::difference_type n = 1); it: Iterator to the base position. difference_type: It is the numerical type that represents distances between iterators of the ForwardIterator type. n: Total no. of positions by which the iterator has to be advanced. In the syntax, n is assigned a default value 1 so it will atleast advance by 1 position. Returns: It returns an iterator to the element n positions away from it. CPP // C++ program to demonstrate std::next #include <iostream> #include <iterator> #include <deque> #include <algorithm> using namespace std; int main() { // Declaring first container deque<int> v1 = { 1, 2, 3, 4, 5, 6, 7 }; // Declaring another container deque<int> v2 = { 8, 9, 10 }; // Declaring an iterator deque<int>::iterator i1; // i1 points to 1 i1 = v1.begin(); // Declaring another iterator to store return // value and using std::next deque<int>::iterator i2; i2 = std::next(i1, 4); // Using std::copy std::copy(i1, i2, std::back_inserter(v2)); // Remember, i1 stills points to 1 // and i2 points to 5 // v2 now contains 8 9 10 1 2 3 4 // Displaying v1 and v2 cout << "v1 = "; int i; for (i = 0; i < 7; ++i) { cout << v1[i] << " "; } cout << "\nv2 = "; for (i = 0; i < 7; ++i) { cout << v2[i] << " "; } return 0; } Output: v1 = 1 2 3 4 5 6 7 v2 = 8 9 10 1 2 3 4 How can it be helpful ? Advancing iterator in Lists: Since, lists support bidirectional iterators, which can be incremented only by using ++ and - - operator. So, if we want to advance the iterator by more than one position, then using std::next can be extremely useful. CPP // C++ program to demonstrate std::next #include <iostream> #include <iterator> #include <list> #include <algorithm> using namespace std; int main() { // Declaring first container list<int> v1 = { 1, 2, 3, 7, 8, 9 }; // Declaring second container list<int> v2 = { 4, 5, 6 }; list<int>::iterator i1; i1 = v1.begin(); // i1 points to 1 in v1 list<int>::iterator i2; // i2 = v1.begin() + 3; // This cannot be used with lists // so use std::next for this i2 = std::next(i1, 3); // Using std::copy std::copy(i1, i2, std::back_inserter(v2)); // v2 now contains 4 5 6 1 2 3 // Displaying v1 and v2 cout << "v1 = "; int i; for (i1 = v1.begin(); i1 != v1.end(); ++i1) { cout << *i1 << " "; } cout << "\nv2 = "; for (i1 = v2.begin(); i1 != v2.end(); ++i1) { cout << *i1 << " "; } return 0; } Output: v1 = 1 2 3 7 8 9 v2 = 4 5 6 1 2 3 Explanation: Here, just look how if we want copy only a selected portion of the list, then we can make use of std::next, as otherwise we cannot use any +=, -= operators with bidirectional iterators supported by lists. So, we used std::next and directly advanced the iterator by three positions. Comment More infoAdvertise with us Next Article std::search in C++ M Mrigendra Singh Improve Article Tags : Misc C++ cpp-iterator STL Practice Tags : CPPMiscSTL Similar Reads std::inserter in C++ std::inserter constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it. It is defined inside the header file . An insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (su 4 min read std::move in C++ std :: move Moves the elements in the range [first,last] into the range beginning at the result. The value of the elements in the [first,last] is transferred to the elements pointed out by the result. After the call, the elements in the range [first,last] are left in an unspecified but valid state. 2 min read std::prev in C++ std::prev returns an iterator pointing to the element after being advanced by certain number of positions in the reverse direction. It is defined inside the header file iterator. It returns a copy of the argument advanced by the specified amount in the backward direction. If it is a random-access it 5 min read std::search in C++ std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence. It searches the sequence [first1, last1) for the first occurrence of the subsequence defi 4 min read find() in C++ STL C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++.C++#include <bits/stdc++.h> using nam 2 min read std::find_end in C++ std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. It is similar to std::se 6 min read Like