| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
constexpr subrange& advance(std::iter_difference_t<I> n); | (since C++20) | |
Increments or decrementsbegin_ :
I modelsbidirectional_iterator andn<0 istrue, decrementsbegin_ by-n elements.begin_ , n);StoreSize ) size_ += to-unsigned-like (-n);begin_ , n, end_ );StoreSize ) size_ -= to-unsigned-like (d);
According to the preconditions ofranges::advance, ifn<0 istrue andbegin_ cannot be decremented by-n elements, the behavior is undefined.
Contents |
| n | - | number of maximal increments of the iterator |
*this
#include <algorithm>#include <array>#include <iostream>#include <iterator>#include <ranges> void print(auto name,autoconst sub){std::cout<< name<<".size() == "<< sub.size()<<"; { "; std::ranges::for_each(sub,[](int x){std::cout<< x<<' ';});std::cout<<"}\n";}; int main(){std::array arr{1,2,3,4,5,6,7}; std::ranges::subrange sub{std::next(arr.begin()),std::prev(arr.end())}; print("1) sub", sub); print("2) sub", sub.advance(3)); print("3) sub", sub.advance(-2));}
Output:
1) sub.size() == 5; { 2 3 4 5 6 }2) sub.size() == 2; { 5 6 }3) sub.size() == 4; { 3 4 5 6 }The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3433 | C++20 | the behavior was undefined ifn<0 | made well-defined ifbegin_ can be decremented |
obtains a copy of thesubrange with its iterator advanced by a given distance(public member function)[edit] | |
obtains a copy of thesubrange with its iterator decremented by a given distance(public member function)[edit] | |
| advances an iterator by given distance (function template)[edit] | |
(C++20) | advances an iterator by given distance or to a given bound (algorithm function object)[edit] |