operator==,!=,<,<=,>,>=,<=>(std::forward_list)
来自 cppreference.cn
< cpp | container | forward list
定义于头文件 <forward_list> |
||
template< class T, class Alloc > bool operator==( const std::forward_list<T, Alloc>& lhs, |
(1) | (自 C++11 起) |
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 > synth-three-way-result<T> |
(7) | (自 C++20 起) |
比较两个 forward_list
的内容。
1,2) 检查 lhs 和 rhs 的内容是否相等,即它们是否拥有相同数量的元素,且 lhs 中的每个元素与 rhs 中相同位置的元素比较相等。
7) 按照字典序比较 lhs 和 rhs 的内容。比较的进行如同调用 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(),
synth-three-way)。 若不满足下列条件之一,则行为未定义
-
T
建模three_way_comparable
。 - 为类型(可能为 const 限定)
T
的值定义了<
,且<
是全序关系。
|
(自 C++20 起) |
内容 |
[edit] 参数
lhs, rhs | - | 要比较其内容的 forward_list |
-为使用重载 (1,2) ,T 必须满足 EqualityComparable 的要求。 | ||
-为使用重载 (3-6) ,T 必须满足 LessThanComparable 的要求。排序关系必须建立全序。 |
[edit] 返回值
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()。
[edit] 复杂度
与 forward_list
的大小成线性关系。
[edit] 注解
关系运算符根据元素类型的 operator< 定义。 |
(C++20 前) |
关系运算符根据 synth-three-way 定义,它尽可能使用 operator<=> ,否则使用 operator< 。 特别地,若元素自身不提供 operator<=> ,但可隐式转换到三路比较类型,则将使用该转换而非 operator< 。 |
(自 C++20 起) |
[edit] 示例
运行此代码
#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 ("" "Compare equal containers:" && (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 && "Compare non equal containers:" && (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 && ""); }
[edit] 缺陷报告
下列行为变更缺陷报告被追溯应用到先前发布的 C++ 标准。
DR | 应用到 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 3431 | C++20 | operator<=> 不要求 T 建模 three_way_comparable |
要求 |