# std::ranges::subrange<I,S,K>::next

> C++ Atlas: `cppatlas.com/w/cpp/ranges/subrange/next` | Source: https://en.cppreference.com/w/cpp/ranges/subrange/next | Domain: Ranges | Standards: C++11, C++20

Returns a subrange whose begin_ is incremented (or decremented if n is negative). The actual increment (or decrement) operation is performed by advance().

## Official Specification Summary (cppreference)

```cpp
constexpr subrange next( std::iter_difference_t<I> n = 1 ) const&
    requires std::forward_iterator<I>; // (1) (since C++20)

constexpr subrange next( std::iter_difference_t<I> n = 1 ) &&; // (2) (since C++20)
```

Returns a `subrange` whose `begin_` is incremented (or decremented if `n` is negative). The actual increment (or decrement) operation is performed by `advance()`.

1) Returns a copy of `*this`.

Equivalent to: `auto tmp = *this;tmp.advance(n);return tmp;`.

2) Returns a `subrange` moved from `*this`.

Equivalent to: `advance(n);return std::move(*this);`.

### Parameter

- `n` - number of maximal increments of the iterator

### Return value

As described above.

### Notes

The difference between this function and `advance()` is that the latter performs the increment (or decrement) in place.

### Example

```cpp
#include <array>
#include <iterator>
#include <print>
#include <ranges>

int main()
{
    std::array arr{1, 2, 3, 4, 5, 6, 7};
    std::ranges::subrange sub{std::next(arr.begin(), 2), std::prev(arr.end(), 2)};
    std::println("1) sub: {}", sub);
    std::println("2) sub: {}", sub.next());
    std::println("3) sub: {}", sub.next(2));
}
```

Output:

```text
1) sub: [3, 4, 5]
2) sub: [4, 5]
3) sub: [5]
```

### See also

- [`prev`](https://en.cppreference.com/w/cpp/ranges/subrange/prev): obtains a copy of the `subrange` with its iterator decremented by a given distance (public member function)
- [`advance`](https://en.cppreference.com/w/cpp/ranges/subrange/advance): advances the iterator by given distance (public member function)
- [`next`](https://en.cppreference.com/w/cpp/iterator/next) (C++11): increment an iterator (function template)
- [`ranges::next`](https://en.cppreference.com/w/cpp/iterator/ranges/next) (C++20): increment an iterator by a given distance or to a bound (algorithm function object)

---
Attribution: content from https://en.cppreference.com/w/cpp/ranges/subrange/next (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.