operator==, <=>(std::counted_iterator)
來自 cppreference.com
template< std::common_with<I> I2 > friend constexpr bool operator==( |
(1) | (C++20 起) |
template< std::common_with<I> I2 > friend constexpr strong_ordering operator<=>( |
(2) | (C++20 起) |
比較底層長度(即到末尾的距離)。
1) 檢查底層長度是否相等。
2) 使用運算子
<=>
比較底層長度。如果 x 和 y 不指向同一序列的元素,則行為未定義。也就是說,必須存在某個 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<=> 在底層比較表示式中的引數順序是反轉的,即 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{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) |
計算兩個迭代器介面卡之間的距離 (函式模板) |