operator==,!=,<,<=,>,>=,<=> (std::forward_list)
|
定义于头文件
<forward_list>
|
||
|
template
<
class
T,
class
Alloc
>
bool
operator
==
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(1) |
(C++11 起)
(C++26 起为 constexpr) |
|
template
<
class
T,
class
Alloc
>
bool
operator
!
=
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(2) |
(C++11 起)
(C++20 前) |
|
template
<
class
T,
class
Alloc
>
bool
operator
<
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(3) |
(C++11 起)
(C++20 前) |
|
template
<
class
T,
class
Alloc
>
bool
operator
<=
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(4) |
(C++11 起)
(C++20 前) |
|
template
<
class
T,
class
Alloc
>
bool
operator
>
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(5) |
(C++11 起)
(C++20 前) |
|
template
<
class
T,
class
Alloc
>
bool
operator
>=
(
const
std::
forward_list
<
T, Alloc
>
&
lhs,
|
(6) |
(C++11 起)
(C++20 前) |
|
template
<
class
T,
class
Alloc
>
/* 见下文 */
|
(7) |
(C++20 起)
(C++26 起为 constexpr) |
比较两个
forward_list
的内容。
令
value_type
为
forward_list
的值类型(即
typename
forward_list
::
value_type
):
|
return
std::
distance
(
lhs.
begin
(
)
, lhs.
end
(
)
)
|
(C++14 前) |
|
return std:: equal ( lhs. begin ( ) , lhs. end ( ) , rhs. begin ( ) , rhs. end ( ) ) ; |
(C++14 起) |
rhs. begin ( ) , rhs. end ( ) ) ; 。
-
value_type不满足 可小于比较 要求。 - operator < 未建立 全序关系 。
rhs.
begin
(
)
, rhs.
end
(
)
,
synth-three-way
)
。
-
T不满足three_way_comparable概念。 -
未为(可能 const 限定的)
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
的大小。
注释
|
关系运算符通过
|
(C++20 前) |
|
关系运算符未定义。重写候选 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
|
要求 |