命名空间
变体
操作

operator==,!=,<,<=,>,>=,<=>(std::reverse_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)
 
 
定义于头文件 <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 起)


目录

[编辑] 参数

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

[编辑] 注意

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” and “y” are equal, but “x” is less than “z” (reversely)
    std::reverse_iterator<int*>
        x{std::rend(a) - std::size(a)},
        y{std::rend(a) - std::size(a)},
        z{std::rbegin(a) + 1};
 
    // two-way comparisons
    assert(  x == y );
    assert(!(x != y));
    assert(!(x <  y));
    assert(  x <= y );
    assert(!(x == z));
    assert(  x != z );
    assert(  x <  z );
    assert(  x <= z );
 
    // three-way comparisons
    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 不允许异类赋值 允许