From cppreference.com
constexprsubrangeprev(std::iter_difference_t<I>n=1)constrequiresstd::bidirectional_iterator<I>;(since C++20)Returns a copy of *this whose
is decremented (or incremented if n is negative). The actual decrement (or increment) operation is performed by
.
Equivalent to:autotmp=*this;
tmp.advance(-n);
returntmp;.
Parameters
n - number of decrements of the iterator Return value
As described above.
Notes
The difference between this function and
is that the latter performs the decrement (or increment) in place.
Example
Run this code
#include<iterator>#include<list>#include<print>#include<ranges>intmain(){std::listlist{1,2,3,4,5};std::ranges::subrangesub{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
obtains a copy of the subrange with its iterator advanced by a given distance
(public member function)
advances the iterator by given distance
(public member function)
(C++11)
decrement an iterator
(function template)
(C++20)
decrement an iterator by a given distance or to a bound
(algorithm function object)