名稱空間
變體
操作

operator==, <=>(std::counted_iterator)

來自 cppreference.com
 
 
迭代器庫
迭代器概念
(C++20)
(C++20)
迭代器原語
(C++17 中已棄用)


演算法概念與工具
間接可呼叫概念
常用演算法要求
(C++20)
(C++20)
(C++20)
工具
(C++20)
迭代器介面卡
範圍訪問
(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 bool operator==(

        const counted_iterator& x, const counted_iterator<I2>& y );
(1) (C++20 起)
template< std::common_with<I> I2 >

    friend constexpr strong_ordering operator<=>(

        const counted_iterator& x, const counted_iterator<I2>& y );
(2) (C++20 起)

比較底層長度(即到末尾的距離)。

1) 檢查底層長度是否相等。
2) 使用運算子 <=> 比較底層長度。

如果 xy 不指向同一序列的元素,則行為未定義。也就是說,必須存在某個 n,使得 std::next(x.base(), x.count() + n)std::next(y.base(), y.count() + n) 引用相同的元素。

<, <=, >, >=!= 運算子分別從 operator<=>operator== 合成

此函式模板對普通的非限定查詢限定查詢不可見,只能透過實參依賴查詢找到,當 std::counted_iterator 是實參的關聯類時。

目錄

[編輯] 引數

x, y - 迭代器介面卡

[編輯] 返回值

1) x.count() == y.count()
2) y.count() <=> x.count()

[編輯] 注意

由於長度是遞減而非遞增的,所以 operator<=> 在底層比較表示式中的引數順序是反轉的,即 ylhsxrhs

[編輯] 示例

#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{v.begin(), 5},
        it3{v.begin() + 1, 4},
        it4{v.begin(), 0};
    static_assert(it1 == it2);
    static_assert(it2 != it3);
    static_assert(it2 <  it3);
    static_assert(it1 <= it2);
    static_assert(it3 != std::default_sentinel);
    static_assert(it4 == std::default_sentinel);
 
//  it2 == std::counted_iterator{v.begin(), 4}; // UB: operands do not refer to
                                                // elements of the same sequence
}

[編輯] 另請參閱

檢查到末尾的距離是否等於 0
(函式模板) [編輯]
(C++20)
前進迭代器
(函式模板) [編輯]
(C++20)
計算兩個迭代器介面卡之間的距離
(函式模板) [編輯]