std::next() is a utility function in the C++ Standard Library that returns a new iterator pointing to an element a specified number of positions away from the given iterator. Unlike std::advance(), it does not modify the original iterator.
- Returns a new iterator while preserving the original iterator.
- Supports forward, bidirectional, and random-access iterators.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> v = {10, 20, 30, 40, 50};
auto it = next(v.begin(), 2);
cout << *it;
return 0;
}
Output
30
Explanation: std::next() returns a new iterator that points two positions after v.begin(), while the original iterator remains unchanged.
Syntax
template <class ForwardIterator>
ForwardIterator next(
ForwardIterator it,
typename iterator_traits<
ForwardIterator>::difference_type n = 1);
Parameters
- it: Iterator to the starting position.
- n: Number of positions by which the iterator should be advanced. The default value is 1.
Return Value: Returns an iterator pointing to the element that is n positions ahead of the given iterator.
Internal Working of std::next()
The implementation of std::next() depends on the iterator category:
- Forward Iterators: Advances the iterator one position at a time.
- Bidirectional Iterators: Supports both forward and backward movement.
- Random-Access Iterators: Uses pointer arithmetic and advances in constant time.
Example: Copying a Range Using std::next()
#include <iostream>
#include <deque>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
deque<int> v1 = {1, 2, 3, 4, 5, 6, 7};
deque<int> v2 = {8, 9, 10};
auto first = v1.begin();
auto last = next(first, 4);
copy(first, last, back_inserter(v2));
cout << "v2 = ";
for (auto x : v2)
cout << x << " ";
return 0;
}
Output
v2 = 8 9 10 1 2 3 4
Explanation: std::next() returns an iterator four positions ahead of v1.begin(), allowing us to copy only a selected portion of the container.
Example: Advancing Iterators in a List
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list<int> l = {1, 2, 3, 7, 8, 9};
auto it = next(l.begin(), 3);
cout << *it;
return 0;
}
Output
7
Explanation: Since std::list provides bidirectional iterators and does not support + operator, std::next() provides a convenient way to move the iterator multiple positions.
Time Complexity
| Iterator Category | Time Complexity |
|---|---|
| Forward Iterator | O(n) |
| Bidirectional Iterator | O(n) |
| Random Access Iterator | O(1) |
Advantages of Using std::next()
std::next() is particularly useful when working with iterators that do not support arithmetic operations.
- Advances an iterator without modifying the original iterator.
- Works with all standard iterator categories.
- Simplifies traversal of containers such as list and forward_list.
- Improves code readability compared to manually incrementing iterators.
Applications of std::next()
std::next() is commonly used when working with STL containers and algorithms that require advancing iterators without modifying the original iterator.
- Selecting a subrange of a container.
- Working with list and forward_list.
- Passing advanced iterators to STL algorithms.
- Writing generic iterator-based code.
Related atricle: std::next() vs std::advance()