operator-(std::counted_iterator)
From cppreference.com
< cpp | iterator | counted iterator
| template< std::common_with<I> I2 > friend constexpr std::iter_difference_t<I2> operator-( |
(C++20以降) | |
2つのイテレータアダプタ間の距離を計算します。
xとyが同じシーケンスの要素を指していない場合、動作は未定義です。つまり、あるnが存在し、std::next(x.base(), x.count() + n)とstd::next(y.base(), y.count() + n)が同じ要素を参照する必要があります。
この関数テンプレートは、通常の非修飾または修飾ルックアップからは見えず、std::counted_iterator<I> が引数の関連クラスである場合にのみ、引数依存の名前探索によって見つけることができます。
目次 |
[編集] パラメータ
| x, y | - | 差を計算するイテレータアダプタ |
[編集] 戻り値
y.count() - x.count()
[編集] 注記
長さは増加するのではなく減少するため、基になる式でのoperator-の引数の順序は逆になります。つまり、yがlhsであり、xがrhsです。
[編集] 例
このコードを実行
#include <initializer_list> #include <iterator> int main() { static constexpr auto v = {1, 2, 3, 4, 5, 6}; constexpr std::counted_iterator<std::initializer_list<int>::iterator> it1{v.begin(), 5}, it2{it1 + 3}, it3{v.begin(), 2}; static_assert(it1 - it2 == -3); static_assert(it2 - it1 == +3); // static_assert(it1 - it3 == -3); // UB: operands of operator- do not refer to // elements of the same sequence }
[編集] 関連項目
counted_iteratorを進める、またはデクリメントする(public member function) | |
| (C++20) |
イテレータを進める (関数テンプレート) |
| 末尾までの符号付き距離を計算する (関数テンプレート) |