Return the nth predecessor of iterator i.
The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
1) The predecessor of i.
2) The nth predecessor of iterator i.
3) The nth predecessor of iterator i, or the first iterator that compares equal to bound, whichever is first.
[edit] Complexity1) Constant.
[edit] Possible implementation [edit] NotesAlthough 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.
#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:
[edit] See also increment an iterator by a given distance or to a boundRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4