operator==,!=,<,<=,>,>=,<=>(std::move_iterator)

来自cppreference.com


 
 
迭代器库
迭代器概念
迭代器原语
算法概念与工具
间接可调用概念
常用算法要求
(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)
 
 
在标头 <iterator> 定义
template< class Iter1, class Iter2 >

bool operator==( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(1) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >

bool operator!=( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(2) (C++17 起为 constexpr)
(C++20 前)
template< class Iter1, class Iter2 >

bool operator< ( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(3) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >

bool operator<=( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(4) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >

bool operator> ( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(5) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >

bool operator>=( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(6) (C++17 起为 constexpr)
template< class Iter1, std::three_way_comparable_with<Iter1> Iter2 >

constexpr std::compare_three_way_result_t<Iter1, Iter2>
    operator<=>( const std::move_iterator<Iter1>& lhs,

                 const std::move_iterator<Iter2>& rhs );
(7) (C++20 起)

比较 lhsrhs 的底层迭代器。

1) 此重载只有在 lhs.base() == rhs.base() 良构并且可转换到 bool 时才会参与重载决议。
3-6) 此重载只有在 lhs.base() < rhs.base() 良构并且可转换到 bool 时才会参与重载决议。

!= 运算符从 operator== 运算符合成

(C++20 起)

目录

[编辑] 参数

lhs, rhs - 要比较的迭代器适配器

[编辑] 返回值

1) lhs.base() == rhs.base()
2) lhs.base() != rhs.base()
3) lhs.base() > rhs.base()
4) lhs.base() >= rhs.base()
5) lhs.base() < rhs.base()
6) lhs.base() <= rhs.base()
7) rhs.base() <=> lhs.base()

[编辑] 示例

#include <cassert>
#include <iterator>
 
int main()
{
    int a[]{9, 8, 7, 6};
    //            │  └───── x, y
    //            └──────── z
 
    // “x” 与 “y” 相等,但 “x” 大于 “z”
    std::move_iterator<int*>
        x{std::end(a) - 1},
        y{std::end(a) - 1},
        z{std::end(a) - 2};
 
    // 双路比较
    assert(  x == y );
    assert(!(x != y));
    assert(!(x <  y));
    assert(  x <= y );
    assert(!(x == z));
    assert(  x != z );
    assert(!(x <  z));
    assert(!(x <= z));
 
    // 三路比较
    assert(  x <=> y == 0 );
    assert(!(x <=> y <  0));
    assert(!(x <=> y >  0));
    assert(!(x <=> z == 0));
    assert(!(x <=> z <  0));
    assert(  x <=> z >  0 );
}

[编辑] 参阅

比较底层迭代器与底层哨位
(函数模板) [编辑]