operator==,<=>(std::counted_iterator)
来自 cppreference.cn
< cpp | iterator | counted iterator
template< std::common_with<I> I2 > friend constexpr bool operator==( |
(1) | (since C++20) |
template< std::common_with<I> I2 > friend constexpr strong_ordering operator<=>( |
(2) | (since 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<I> 是参数的关联类时,才能通过实参依赖查找找到它。
目录 |
[编辑] 参数
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) |
计算两个迭代器适配器之间的距离 (函数模板) |