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)(C++11 起)
template< class T, class Alloc >

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

                 const std::forward_list<T, Alloc>& rhs );
(2)(C++11 起)
(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++11 起)
(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++11 起)
(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++11 起)
(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++11 起)
(C++20 前)
template< class T, class Alloc >

synth-three-way-result<T>
    operator<=>( const std::forward_list<T, Alloc>& lhs,

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

比较两个 forward_list 的内容。

1,2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
3-6) 按字典序比较 lhsrhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。
7) 按字典序比较 lhsrhs 的内容。如同通过调用 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                       rhs.begin(), rhs.end(), synth-three-way)
进行比较。
返回类型是 synth-three-way 的返回类型(即 synth-three-way-result <T>)。
如果不满足以下任意条件,那么行为未定义:
  • T 实现了 three_way_comparable
  • 为(可有 const 限定的)T 类型的值定义了 <,并且 < 是全序关系。

<<=>>=!= 运算符分别从 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)lhsrhs 中的首对不等价元素的相对顺序,如果有这种元素;否则是 lhs.size() <=> rhs.size()

复杂度

forward_list 的大小成线性

注解

各关系运算符是基于各个元素的 operator< 定义的。

(C++20 前)

各关系运算符是基于 synth-three-way 定义的,若有可能则它会使用 operator<=>,否则使用 operator<

要注意,如果元素类型自身并不提供 operator<=>,但可以隐式转换为可三路比较的类型,则就会用这项转换来替代 operator<

(C++20 起)

示例

#include <cassert>
#include <compare>
#include <forward_list>
 
int main()
{
    const std::forward_list
        a{1, 2, 3},
        b{1, 2, 3},
        c{7, 8, 9, 10};
 
    assert
    (""
        "比较相等的容器:" &&
        (a != b) == false &&
        (a == b) == true &&
        (a < b) == false &&
        (a <= b) == true &&
        (a > b) == false &&
        (a >= b) == true &&
        (a <=> b) != std::weak_ordering::less &&
        (a <=> b) != std::weak_ordering::greater &&
        (a <=> b) == std::weak_ordering::equivalent &&
        (a <=> b) >= 0 &&
        (a <=> b) <= 0 &&
        (a <=> b) == 0 &&
 
        "比较不相等的容器:" &&
        (a != c) == true &&
        (a == c) == false &&
        (a < c) == true &&
        (a <= c) == true &&
        (a > c) == false &&
        (a >= c) == false &&
        (a <=> c) == std::weak_ordering::less &&
        (a <=> c) != std::weak_ordering::equivalent &&
        (a <=> c) != std::weak_ordering::greater &&
        (a <=> c) < 0 &&
        (a <=> c) != 0 &&
        (a <=> c) <= 0 &&
    "");
}

缺陷报告

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

缺陷报告应用于出版时的行为正确行为
LWG 3431C++20operator<=> 不需要 T 实现 three_way_comparable需要