◐ Shell
clean mode source ↗

operator==,!=,<,<=,>,>=,<=>(std::reverse_iterator) - cppreference.com

来自cppreference.com

在标头 <iterator> 定义

template< class Iter1, class Iter2 >
bool operator==( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(1) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >
bool operator!=( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(2) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >
bool operator< ( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(3) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >
bool operator<=( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(4) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >
bool operator> ( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(5) (C++17 起为 constexpr)
template< class Iter1, class Iter2 >
bool operator>=( const std::reverse_iterator<Iter1>& lhs,
                 const std::reverse_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::reverse_iterator<Iter1>& lhs,
                 const std::reverse_iterator<Iter2>& rhs );
(7) (C++20 起)

比较 lhsrhs 的底层迭代器。

  • 相等性比较的结果按原样保留(即底层迭代器相等意味着逆向迭代器也相等)。
  • 关系比较的结果会反转(即底层迭代器更大意味着逆向迭代器会更小)。

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

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

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

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

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

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

(C++20 起)

参数

返回值

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()

注解

因为这是逆向迭代器,所以 operator<=> 返回的是 rhs.base() <=> lhs.base() 而不是 lhs.base() <=> rhs.base()

示例

#include <cassert>
#include <iterator>

int main()
{
    int a[]{0, 1, 2, 3};
    //            ↑  └───── x, y
    //            └──────── z

    // “x” 与 “y” 相等,但 “x”(逆向)小于 “z”
    std::reverse_iterator<int*>
        x{std::rend(a) - std::size(a)},
        y{std::rend(a) - std::size(a)},
        z{std::rbegin(a) + 1};
    
    // 双路比较
    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));
}

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 280 C++98 不允许异质比较 允许异质比较