
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ iterator::next() Function
The C++ iterator::next() function returns an iterator that points to the element you got after incrementing the iterator pointer from the current element. It returns a copy of the argument that has been advanced by the specified amount without changing the original argument.
The function only employs operator+ or operator- once if it is a random-access iterator. Otherwise, until n elements have been advanced, the function repeatedly applies the increase or decrease operator (operator++ or operator) to the copied iterator.
Syntax
Following is the syntax for C++ iterator::next() Function −
ForwardIterator next (ForwardIterator it, typename iterator_traits<ForwardIterator>::difference_type n = 1);
Parameters
- it − it indicates the current position.
- n − it indicates the number of times it have to be iterate.
Example 1
Let's consider the following example, where we are going to use the next() function and retrieving the output.
#include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> tutorial{ 11,22,33,44 }; auto it = tutorial.begin(); auto nx = std::next(it,1); std::cout << *it << ' ' << *nx << '\n'; }
Output
When we compile and run the above program, this will produce the following result −
11 22
Example 2
In the following example, we are going to declare to array and apply next() function to the second array and which returns the complete second array along with four elements of first array.
#include <iostream> #include <iterator> #include <list> #include <algorithm> using namespace std; int main() { list<int> t1 = {123,234,345,456,567,678}; list<int> t2 = { 11,22,33,44}; list<int>::iterator i1; i1 = t1.begin(); list<int>::iterator i2; i2 = std::next(i1, 4); std::copy(i1, i2, std::back_inserter(t2)); cout << "\nResult = "; for (i1 = t2.begin(); i1 != t2.end(); ++i1) { cout << *i1 << " "; } return 0; }
Output
On running the above program, it will produce the following result −
Result = 11 22 33 44 123 234 345 456
Example 3
Considering the following example, where we are going to run the loop and applying the next() function to retrieving the output.
#include <iostream> #include <iterator> #include <list> #include <algorithm> int main () { std::list<int> mylist; for (int i = 0; i < 10; i++) mylist.push_back (i*1); std::cout << "mylist:"; std::for_each (mylist.begin(), std::next(mylist.begin(),4), [](int x) { std::cout << ' ' << x; } ); std::cout << '\n'; return 0; }
Output
On running the above program, it will produce the following result −
mylist: 0 1 2 3