operator==,<=>(std::inplace_vector)
来自 cppreference.cn
< cpp | 容器 | inplace vector
constexpr friend bool operator==( const std::inplace_vector<T, N>& lhs, const std::inplace_vector<T, N>& rhs ); |
(1) | (since C++26) |
constexpr friend synth-three-way-result<T> operator<=>( const std::inplace_vector<T, N>& lhs, |
(2) | (since C++26) |
比较两个 std::inplace_vector 的内容。
1) 检查 lhs 和 rhs 的内容是否相等,即它们是否具有相同数量的元素,并且 lhs 中的每个元素与 rhs 中相同位置的元素比较相等。
2) 按照字典顺序比较 lhs 和 rhs 的内容。 比较的执行方式如同调用
std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(), synth-three-way);.
std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(), synth-three-way);.
必须满足以下条件之一
-
T
模型three_way_comparable
。 - 为
T
类型(可能带有 const 限定)的值定义了<
,并且<
是全序关系。
<
、<=
、>
、>=
和 !=
运算符分别从 operator<=> 和 operator== 合成。
目录 |
[编辑] 参数
lhs, rhs | - | std::inplace_vector,要比较其内容 |
-T 必须满足 EqualityComparable 的要求才能使用重载 (1)。 |
[编辑] 返回值
1) 如果 std::inplace_vector 的内容相等,则为 true,否则为 false。
2) 如果存在不相等的元素对,则为 lhs 和 rhs 中第一对不相等元素的相对顺序,否则为 lhs.size() <=> rhs.size()。
[编辑] 复杂度
1) 如果 lhs 和 rhs 的大小不同,则为常数时间,否则与 std::inplace_vector 的大小呈线性关系。
2) 与 std::inplace_vector 的大小呈线性关系。
[编辑] 注解
关系运算符根据 synth-three-way 定义,后者尽可能使用 operator<=>,否则使用 operator<。
值得注意的是,如果元素本身不提供 operator<=>,但可以隐式转换为三路可比较类型,则将使用该转换而不是 operator<。
[编辑] 示例
运行此代码
#include <inplace_vector> int main() { constexpr std::inplace_vector<int, 4> a{1, 2, 3}, b{1, 2, 3}, c{7, 8, 9, 10}; static_assert ("" "Compare equal containers:" && (a != b) == false && (a == b) == true && (a < b) == false && (a <= b) == true && (a > b) == false && (a >= b) == true && (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) < 0 && (a <=> c) != 0 && (a <=> c) <= 0 && ""); }