Namespaces
Variants

std::ranges::next

From cppreference.com
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
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:

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.
2) Linear in |count|, or constant if I models random_access_iterator.
3) Linear in ranges::distance(iter, bound), or constant if any of the following conditions is satisfied:
  • I and S model std::assignable_from<I&, S>.
  • S and I model std::sized_sentinel_for<S, I>, and I models 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|, and I models random_access_iterator.
  • |count|≥|bound - iter|, and any of the following conditions is satisfied:
    • I and S model std::assignable_from<I&, S>.
    • S and I model std::sized_sentinel_for<S, I>, and I models 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

#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) [edit]
decrement an iterator by a given distance or to a bound
(algorithm function object)[edit]
advances an iterator by given distance or to a given bound
(algorithm function object)[edit]