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

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

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

## Official Specification Summary (cppreference)

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

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

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

### Parameters

- `n` - number of decrements of the iterator

### Return value

As described above.

### Notes

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

### Example

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

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

Output:

```text
[3] [2, 3] [1, 2, 3]
```

### See also

- [`next`](https://en.cppreference.com/w/cpp/ranges/subrange/next): obtains a copy of the `subrange` with its iterator advanced 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)
- [`prev`](https://en.cppreference.com/w/cpp/iterator/prev) (C++11): decrement an iterator (function template)
- [`ranges::prev`](https://en.cppreference.com/w/cpp/iterator/ranges/prev) (C++20): decrement an iterator by a given distance or to a bound (algorithm function object)

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