On this page
std::ranges::prev
Defined in header <iterator> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
Return the n
th predecessor of iterator i
.
The function-like entities described on this page are 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.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
i | - | an iterator |
n | - | number of elements i should be descended |
bound | - | iterator denoting the beginning of the range i points to |
Return value
i
.
n
th predecessor of iterator i
.
n
th predecessor of iterator i
, or the first iterator that compares equal to bound
, whichever is first.
Complexity
Possible implementation
|
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 ranges::common_range
. For example, for some underlying ranges, ranges::transform_view::end
doesn't have the same return type as ranges::transform_view::begin
, and so --r.end()
won't compile. This isn't 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++20)
|
increment an iterator by a given distance or to a bound (niebloid) |
(C++20)
|
advances an iterator by given distance or to a given bound (niebloid) |
(C++11)
|
decrement an iterator (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/ranges/prev