名前空間
変種
操作

operator-(std::counted_iterator)

From cppreference.com
 
 
イテレータライブラリ
イテレータのコンセプト
イテレータのプリミティブ
アルゴリズムのコンセプトとユーティリティ
間接呼び出し可能コンセプト
共通アルゴリズム要件
(C++20)
(C++20)
(C++20)
ユーティリティ
(C++20)
イテレータアダプタ
Rangeアクセス
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
template< std::common_with<I> I2 >

    friend constexpr std::iter_difference_t<I2> operator-(

        const counted_iterator& x, const counted_iterator<I2>& y );
(C++20以降)

2つのイテレータアダプタ間の距離を計算します。

xyが同じシーケンスの要素を指していない場合、動作は未定義です。つまり、ある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-の引数の順序は逆になります。つまり、ylhsであり、xrhsです。

[編集]

#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)
イテレータを進める
(関数テンプレート) [編集]
末尾までの符号付き距離を計算する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)