Scroll to navigation

std::ranges::subrange::prev(3) C++ Standard Libary std::ranges::subrange::prev(3)

NAME

std::ranges::subrange::prev - std::ranges::subrange::prev

Synopsis

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

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

// Run this code

#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:

[3] [2, 3] [1, 2, 3]

See also

next obtains a copy of the subrange with its iterator advanced by a given distance
(public member function) advance advances the iterator by given distance
(public member function) prev decrement an iterator (C++11) (function template) ranges::prev decrement an iterator by a given distance or to a bound (C++20) (algorithm function object)

2026.11.10 http://cppreference.com