Namespaces
Variants

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

From cppreference.net

定义于头文件 <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 起)
(C++26 起为 constexpr)
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 >

/* 见下文 */
operator <=> ( const std:: forward_list < T, Alloc > & lhs,

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

比较两个 forward_list 的内容。

value_type forward_list 的值类型(即 typename forward_list :: value_type ):

1,2) 检查 lhs rhs 的内容是否相等,即它们是否具有相同数量的元素,且 lhs 中的每个元素都与 rhs 中相同位置的元素比较相等。
等价于:

return std:: distance ( lhs. begin ( ) , lhs. end ( ) )
== std:: distance ( rhs. begin ( ) , rhs. end ( ) )
&& std:: equal ( lhs. begin ( ) , lhs. end ( ) , rhs. begin ( ) ) ;

(C++14 前)

return std:: equal ( lhs. begin ( ) , lhs. end ( ) , rhs. begin ( ) , rhs. end ( ) ) ;

(C++14 起)
value_type 不满足 EqualityComparable 要求,则行为未定义。
3-7) 按字典序比较 lhs rhs 的内容。
3-6) 等价于 return std:: lexicographical_compare ( lhs. begin ( ) , lhs. end ( ) ,
rhs. begin ( ) , rhs. end ( ) ) ;
若满足以下任一条件,则行为未定义:
  • value_type 不满足 可小于比较 要求。
  • operator < 未建立 全序关系
7) 等价于 return std:: lexicographical_compare_three_way ( lhs. begin ( ) , lhs. end ( ) ,
rhs. begin ( ) , rhs. end ( ) ,
synth-three-way )
返回类型为 synth-three-way 的返回类型(即 synth-three-way-result  < value_type > )。
若满足以下任一条件,则行为未定义:

< <= > >= != 运算符分别由 operator <=> operator == 自动合成。

(since C++20)

目录

参数

lhs, rhs - 要比较内容的 forward_list 对象

返回值

运算符 lhs rhs
相等
lhs
字典序更大
rhs
字典序更大
operator == true false
operator ! = false true
operator < false false true
operator <= true
operator > false true false
operator >= true
operator <=> 等于 0 的值 大于 0 的值 小于 0 的值

复杂度

线性于 forward_list 的大小。

注释

关系运算符通过 value_type operator < 定义。

(C++20 前)

关系运算符未定义。重写候选 operator <=> 将通过重载决议被选择。

operator <=> 优先使用 value_type operator <=> ,否则回退至 value_type operator < 。特别地,若 value_type 本身未提供 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 3431 C++20 operator <=> 未要求 T
建模 three_way_comparable
要求