std::next(3)
π
std::next(3) β C++ Standard Libary, bestie π
okay so welcome to std::next, the iterator-advancing icon of the C++ Standard Library. This is the std::next(3) manual page, and we're gonna get you comfy with it. no cap. β¨
πΈ NAME πΈ
std::next - std::next
β¨ Synopsis β¨
Defined in header <iterator>.
The page lays out the synopsis like this, version labels and all β I'm keeping it exactly as it appears:
Defined in header <iterator>
template< class InputIt >
(since C++11)
InputIt next( InputIt it, typename
(until C++17)
std::iterator_traits<InputIt>::difference_type n = 1 );
template< class InputIt >
constexpr
(since C++17)
InputIt next( InputIt it, typename
std::iterator_traits<InputIt>::difference_type n = 1 );
Return the n^th successor (or -n^th predecessor if n is negative) of iterator it.
π Parameters π
itβ an iteratornβ number of elements to advance
π Type requirements π
InputItmust meet the requirements ofLegacyInputIterator.
π¦ Return value π¦
An iterator of type InputIt that holds the n^th successor (or -n^th predecessor if n is negative) of iterator it.
β±οΈ Complexity β±οΈ
Linear.
However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator, complexity is constant.
π οΈ Possible implementation π οΈ
template<class InputIt>
constexpr // since C++17
InputIt next(InputIt it, typename
std::iterator_traits<InputIt>::difference_type n = 1)
{
std::advance(it, n);
return it;
}
π Notes π
Although the expression ++c.begin() often compiles, it is not guaranteed to do so: c.begin() is an rvalue expression, and there is no LegacyInputIterator requirement that specifies that increment of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers or its operator++ is lvalue-ref-qualified, ++c.begin() does not compile, while std::next(c.begin()) does.
π Example π
The page says // Run this code right before the example:
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v{4, 5, 6};
auto it = v.begin();
auto nx = std::next(it, 2);
std::cout << *it << ' ' << *nx << '\n';
it = v.end();
nx = std::next(it, -2);
std::cout << ' ' << *nx << '\n';
}
π₯οΈ Output:
4 6
5
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 2353β applied toC++11β behavior as published:nextrequiredLegacyForwardIterator; correct behavior:LegacyInputIteratorallowed.
π See also π
prev(C++11) (function template) β decrement an iteratoradvance(function template) β advances an iterator by given distancedistance(function template) β returns the distance between two iteratorsranges::next(C++20) (niebloid) β increment an iterator by a given distance or to a bound
okay that's the whole page, bestie. this one came from http://cppreference.com, dated 2024.06.10, and it's the std::next(3) page. go advance some iterators responsibly πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms