operator==,<=>(std::inplace_vector)
来自 zh.cppreference.net
constexpr friend bool operator==( const std::inplace_vector<T, N>& lhs,
const std::inplace_vector<T, N>& rhs );
|
(1) | (自 C++26 起) |
constexpr friend synth-three-way-result<T>
operator<=>( const std::inplace_vector<T, N>& lhs,
const std::inplace_vector<T, N>& rhs );
|
(2) | (自 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*/);
必须满足以下至少一个条件:
T模型 three_way_comparable。<对于(可能 const 限定的)T类型的值有定义,且<是全序关系。
运算符 <、<=、>、>= 和 != 分别由 synthesized 通过 operator<=> 和 operator== 合成。
参数
| lhs, rhs | - | 待比较内容的 std::inplace_vector 对象 |
-
T
必须满足
EqualityComparable
要求才能使用重载 (1)
|
||
返回值
1)
true
如果
std::inplace_vector
的内容相等,否则为
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 &&
"");
}