std::ranges::next
From cppreference.com
| Defined in header <iterator>
|
||
| Call signature |
||
template< std::input_or_output_iterator I >
constexpr I next( I iter );
|
(1) | (since C++20) |
template< std::input_or_output_iterator I >
constexpr I next( I iter, std::iter_difference_t<I> count );
|
(2) | (since C++20) |
template< std::input_or_output_iterator I, std::sentinel_for<I> S >
constexpr I next( I iter, S bound );
|
(3) | (since C++20) |
template< std::input_or_output_iterator I, std::sentinel_for<I> S >
constexpr I next( I iter, std::iter_difference_t<I> count, S bound );
|
(4) | (since C++20) |
Increments the given iterator iter and returns the incremented iterator.
| Overload | Return value | Equivalent to |
|---|---|---|
| (1) | the successor of iter
|
++iter;return iter;
|
| (2) | the countth successor of iter
|
ranges::advance(iter, count);return iter;
|
| (3) | the first iterator equivalent to bound
|
ranges::advance(iter, bound);return iter;
|
| (4) | the
countth successor of iter, or the first iterator equivalent to bound, whichever is first |
ranges::advance(iter, count, bound);return iter;
|
The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
Parameters
| iter | - | an iterator |
| count | - | the maximum number of steps to increment |
| bound | - | the boundary of incrementation |
Return value
As described above.
Complexity
1) Constant.
3) Linear in
ranges::distance(iter, bound), or constant if any of the following conditions is satisfied:
IandSmodelstd::assignable_from<I&, S>.SandImodelstd::sized_sentinel_for<S, I>, andImodels random_access_iterator.
4) Linear in the lesser of the absolute values of
count and ranges::distance(iter, bound), or constant if S and I model std::sized_sentinel_for<S, I> and one of the following conditions is satisfied:
- |
count|<|bound - iter|, andImodels random_access_iterator. - |
count|≥|bound - iter|, and any of the following conditions is satisfied:IandSmodelstd::assignable_from<I&, S>.SandImodelstd::sized_sentinel_for<S, I>, andImodels random_access_iterator.
Possible implementation
struct next_fn
{
template<std::input_or_output_iterator I>
constexpr I operator()(I iter) const
{
++iter;
return iter;
}
template<std::input_or_output_iterator I>
constexpr I operator()(I iter, std::iter_difference_t<I> count) const
{
ranges::advance(iter, count);
return iter;
}
template<std::input_or_output_iterator I, std::sentinel_for<I> S>
constexpr I operator()(I iter, S bound) const
{
ranges::advance(iter, bound);
return iter;
}
template<std::input_or_output_iterator I, std::sentinel_for<I> S>
constexpr I operator()(I i, std::iter_difference_t<I> count, S bound) const
{
ranges::advance(iter, count, bound);
return iter;
}
};
inline constexpr auto next = next_fn();
|
Notes
Although the expression ++x.begin() often compiles, it is not guaranteed to do so: x.begin() is an rvalue expression, and there is no 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, ++x.begin() does not compile, while ranges::next(x.begin()) does.
Example
Run this code
#include <cassert>
#include <iterator>
int main()
{
auto v = {3, 1, 4};
auto n1 = std::ranges::next(v.begin());
assert(*n1 == 1);
auto n2 = std::ranges::next(v.begin(), 2);
assert(*n2 == 4);
auto n3 = std::ranges::next(v.begin(), v.end());
assert(n3 == v.end());
auto n4 = std::ranges::next(v.begin(), 42, v.end());
assert(n4 == v.end());
}
See also
(C++11) |
increment an iterator (function template) |
(C++20) |
decrement an iterator by a given distance or to a bound (algorithm function object) |
(C++20) |
advances an iterator by given distance or to a given bound (algorithm function object) |