std::ranges::prev
| 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:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
Parameters
| iter | - | an iterator |
| count | - | the maximum number of steps to decrement |
| bound | - | the boundary of decrementation |
Return value
As described above.
Complexity
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|, andImodels random_access_iterator. - |
count|≥|bound - iter|, and any of the following conditions is satisfied:IandSmodelstd::assignable_from<I&, S>.SandImodelstd::sized_sentinel_for<S, I>, andImodels 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) |
(C++20) |
increment an iterator by a given distance or to a bound (algorithm function object) |
(C++20) |
advances an iterator by given distance or to a given bound (algorithm function object) |