Namespaces
Variants

std::ranges::prev

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::bidirectional_iterator I >
constexpr I prev( I iter );
(1) (since C++20)
template< std::bidirectional_iterator I >
constexpr I prev( I iter, std::iter_difference_t<I> count );
(2) (since C++20)
template< std::bidirectional_iterator I >
constexpr I prev( I iter, std::iter_difference_t<I> count, I bound );
(3) (since C++20)

Decrements the given iterator iter and returns the decremented iterator.

 Overload  Return value Equivalent to
(1) the predecessor of iter ++iter;
return iter;
(2) the countth predecessor of iter ranges::advance(iter, -count);
return iter;
(3)
the countth predecessor 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 decrement
bound - the boundary of decrementation

Return value

As described above.

Complexity

1) Constant.
2) Linear in |count|, or constant if I models random_access_iterator.
3) 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 prev_fn
{
    template<std::bidirectional_iterator I>
    constexpr I operator()(I iter) const
    {
        --iter;
        return iter;
    }

    template<std::bidirectional_iterator I>
    constexpr I operator()(I iter, std::iter_difference_t<I> count) const
    {
        ranges::advance(iter, -count);
        return iter;
    }

    template<std::bidirectional_iterator I>
    constexpr I operator()(I iter, std::iter_difference_t<I> count, I bound) const
    {
        ranges::advance(iter, -count, bound);
        return iter;
    }
};

inline constexpr auto prev = prev_fn();

Notes

Although the expression --r.end() often compiles for containers, it is not guaranteed to do so: r.end() is an rvalue expression, and there is no iterator requirement that specifies that decrement of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers or its operator-- is lvalue-ref-qualified, --r.end() does not compile, while ranges::prev(r.end()) does.

This is further exacerbated by ranges that do not model common_range. For example, for some underlying ranges, ranges::transform_view::end does not have the same return type as ranges::transform_view::begin, and so --r.end() will not compile. This is not something that ranges::prev can aid with, but there are workarounds.

Example

#include <iostream>
#include <iterator>
#include <vector>

int main() 
{
    std::vector<int> v{3, 1, 4};
    auto pv = std::ranges::prev(v.end(), 2);
    std::cout << *pv << '\n';
    
    pv = std::ranges::prev(pv, 42, v.begin());
    std::cout << *pv << '\n';
}

Output:

1
3

See also

(C++11)
decrement an iterator
(function template) [edit]
increment 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]