std::ranges::subrange<I,S,K>::advance
ja.cppreference.net から
constexpr subrange& advance( std::iter_difference_t<I> n );
|
(C++20 以降) | |
以下のようにインクリメントまたはデクリメントするbegin_ :
- もし
Iが bidirectional_iterator をモデル化し、かつn < 0がtrueであるならば、begin_を-n要素分デクリメントする。
- 以下と同等:
ranges::advance(begin_, n);if constexpr (StoreSize)size_+=to-unsigned-like(-n);return *this;.
- 以下と同等:
auto d = n - ranges::advance(begin_, n,end_);if constexpr (StoreSize)size_-=to-unsigned-like(d);return *this;.
ranges::advance の事前条件によれば、もし n < 0 が true であり、かつ begin_ を -n 要素分デクリメントできない場合、動作は未定義である。
パラメータ
| n | - | イテレータの最大増分数 |
戻り値
* this
例
このコードを実行
#include <algorithm> #include <array> #include <iostream> #include <iterator> #include <ranges> void print(auto name, auto const 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)); }
出力:
1) sub.size() == 5; { 2 3 4 5 6 }
2) sub.size() == 2; { 5 6 }
3) sub.size() == 4; { 3 4 5 6 }
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3433 | C++20 | n < 0 の場合、動作は未定義であった |
begin_
がデクリメント可能な場合、明確に定義された
|
関連項目
指定された距離だけイテレータを進めた
subrange
のコピーを取得する
(公開メンバ関数) |
|
指定された距離だけイテレータを戻した
subrange
のコピーを取得する
(公開メンバ関数) |
|
|
イテレータを指定された距離だけ進める
(関数テンプレート) |
|
|
(C++20)
|
イテレータを指定された距離だけ、または指定された境界まで進める
(アルゴリズム関数オブジェクト) |