命名空间
变体
操作

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

来自 cppreference.cn
 
 
迭代器库
迭代器概念
迭代器原语
算法概念和工具
间接可调用概念
常用算法要求
(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) (since C++20)
template< std::common_with<I> I2 >

    friend constexpr strong_ordering operator<=>(

        const counted_iterator& x, const counted_iterator<I2>& y );
(2) (since 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<I> 是参数的关联类时,才能通过实参依赖查找找到它。

目录

[编辑] 参数

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)
计算两个迭代器适配器之间的距离
(函数模板) [编辑]