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

来自cppreference.com

 
 
 
 
在标头 <forward_list> 定义
template< class T, class Alloc >

bool operator==( const std::forward_list<T, Alloc>& lhs,

                 const std::forward_list<T, Alloc>& rhs );
(1)
template< class T, class Alloc >

bool operator!=( const std::forward_list<T, Alloc>& lhs,

                 const std::forward_list<T, Alloc>& rhs );
(2) (C++20 前)
template< class T, class Alloc >

bool operator<( const std::forward_list<T, Alloc>& lhs,

                const std::forward_list<T, Alloc>& rhs );
(3) (C++20 前)
template< class T, class Alloc >

bool operator<=( const std::forward_list<T, Alloc>& lhs,

                 const std::forward_list<T, Alloc>& rhs );
(4) (C++20 前)
template< class T, class Alloc >

bool operator>( const std::forward_list<T, Alloc>& lhs,

                const std::forward_list<T, Alloc>& rhs );
(5) (C++20 前)
template< class T, class Alloc >

bool operator>=( const std::forward_list<T, Alloc>& lhs,

                 const std::forward_list<T, Alloc>& rhs );
(6) (C++20 前)
template< class T, class Alloc >

/* see below */ operator<=>( const std::forward_list<T, Alloc>& lhs,

                             const std::forward_list<T, Alloc>& rhs );
(7) (C++20 起)

比较两个 forward_list 的内容。

1,2) 检查 lhs 与 rhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
3-6) 按字典序比较 lhs 与 rhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。
7) 按字典序比较 lhs 与 rhs 的内容。如同通过在两个 forward_list 上以进行合成三路比较(见后述)的函数对象调用 std::lexicographical_compare_three_way 进行比较。返回类型同合成三路比较的结果类型。

给定分别作为左操作数与右操作数的两个 const E 左值 lhs 与 rhs(其中 E 是 T),合成三路比较定义如下:

  • 在 std::three_way_comparable_with<E, E> 得到满足时等价于 lhs <=> rhs;
  • 否则,在以 operator< 比较两个 const E 左值良构且结果类型满足 boolean-testable 时等价于
lhs < rhs ? std::weak_ordering::less :
rhs < lhs ? std::weak_ordering::greater :
            std::weak_ordering::equivalent
  • 否则,不定义合成三路比较,而且 operator<=> 不会参与重载决议。
如果 three_way_comparable_with 或 boolean-testable 被满足但未被实现,或使用 operator< 但 E 与 < 不建立全序,那么 operator<=> 的行为未定义。

<、 <=、 >、 >= 及 != 运算符分别从 operator<=> 与 operator== 合成。

(C++20 起)

参数

lhs, rhs - 要比较内容的 forward_list
-
为使用重载 (1,2), T 必须符合可相等比较 (EqualityComparable) 的要求。
-
为使用重载 (3-6), T 必须符合可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 在 forward_list 内容相等时返回 true,否则返回 false
2) 在 forward_list 内容不相等时返回 true,否则返回 false
3) 在 lhs 的内容按字典序小于 rhs 的内容时返回 true,否则返回 false
4) 在 lhs 的内容按字典序小于或等于 rhs 的内容时返回 true,否则返回 false
5) 在 lhs 的内容按字典序大于 rhs 的内容时返回 true,否则返回 false
6) 在 lhs 的内容按字典序大于或等于 rhs 的内容时返回 true,否则返回 false
7) lhs 与 rhs 中的首对不等价元素的相对顺序,如果有这种元素;否则是 lhs.size() <=> rhs.size()。

复杂度

与 forward_list 的大小呈线性

示例

#include <iostream>
#include <forward_list>
 
int main()
{
    std::forward_list<int> alice{1, 2, 3};
    std::forward_list<int> bob{7, 8, 9, 10};
    std::forward_list<int> eve{1, 2, 3};
 
    std::cout << std::boolalpha;
 
    // 比较不相等的容器
    std::cout << "alice == bob returns " << (alice == bob) << '\n';
    std::cout << "alice != bob returns " << (alice != bob) << '\n';
    std::cout << "alice <  bob returns " << (alice < bob) << '\n';
    std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
    std::cout << "alice >  bob returns " << (alice > bob) << '\n';
    std::cout << "alice >= bob returns " << (alice >= bob) << '\n';
 
    std::cout << '\n';
 
    // 比较相等的容器
    std::cout << "alice == eve returns " << (alice == eve) << '\n';
    std::cout << "alice != eve returns " << (alice != eve) << '\n';
    std::cout << "alice <  eve returns " << (alice < eve) << '\n';
    std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
    std::cout << "alice >  eve returns " << (alice > eve) << '\n';
    std::cout << "alice >= eve returns " << (alice >= eve) << '\n';
}

输出:

alice == bob returns false
alice != bob returns true
alice <  bob returns true
alice <= bob returns true
alice >  bob returns false
alice >= bob returns false
 
alice == eve returns true
alice != eve returns false
alice <  eve returns false
alice <= eve returns true
alice >  eve returns false
alice >= eve returns true