命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 容器‎ | set
 
 
 
 
在头文件 <set> 中定义
template< class Key, class Compare, class Alloc >

bool operator==( const std::set<Key, Compare, Alloc>& lhs,

                 const std::set<Key, Compare, Alloc>& rhs );
(1)
template< class Key, class Compare, class Alloc >

bool operator!=( const std::set<Key, Compare, Alloc>& lhs,

                 const std::set<Key, Compare, Alloc>& rhs );
(2) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator<( const std::set<Key, Compare, Alloc>& lhs,

                const std::set<Key, Compare, Alloc>& rhs );
(3) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator<=( const std::set<Key, Compare, Alloc>& lhs,

                 const std::set<Key, Compare, Alloc>& rhs );
(4) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator>( const std::set<Key, Compare, Alloc>& lhs,

                const std::set<Key, Compare, Alloc>& rhs );
(5) (C++20 前)
template< class Key, class Compare, class Alloc >

bool operator>=( const std::set<Key, Compare, Alloc>& lhs,

                 const std::set<Key, Compare, Alloc>& rhs );
(6) (C++20 前)
template< class Key, class Compare, class Alloc >

synth-three-way-result<T>
    operator<=>( const std::set<Key, Compare, Alloc>& lhs,

                 const std::set<Key, Compare, Alloc>& rhs );
(7) (C++20 起)

比较两个 set 的内容。

1,2) 检查 lhsrhs 的内容是否相等,即它们拥有相同数量的元素,并且 lhs 中的每个元素与 rhs 中相同位置的元素比较相等。
3-6) 按字典序比较 lhsrhs 的内容。比较通过等价于 std::lexicographical_compare 的函数执行。此比较忽略 set 的排序 Compare
7) 按字典序比较 lhsrhs 的内容。比较的执行如同调用 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                       rhs.begin(), rhs.end(), synth-three-way)
。此比较忽略 set 的排序 Compare
返回类型是 synth-three-way 的返回类型(即 synth-three-way-result <T>)。
如果以下条件均不满足,则行为未定义
  • T 满足 three_way_comparable
  • 为类型(可能 const 限定的)T 的值定义了 <,并且 < 是全序关系。

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

(C++20 起)

目录

[编辑] 参数

lhs, rhs - 待比较内容的 set
-
为了使用重载 (1,2),Key 必须满足 EqualityComparable 的要求。

[编辑] 返回值

1)set 的内容相等,则为 true,否则为 false
2)set 的内容不相等,则为 true,否则为 false
3)lhs 的内容按字典序小于 rhs 的内容,则为 true,否则为 false
4)lhs 的内容按字典序小于或等于 rhs 的内容,则为 true,否则为 false
5)lhs 的内容按字典序大于 rhs 的内容,则为 true,否则为 false
6)lhs 的内容按字典序大于或等于 rhs 的内容,则为 true,否则为 false
7)lhsrhs 存在第一对不等价元素,则返回它们的相对顺序,否则返回 lhs.size() <=> rhs.size()

[编辑] 复杂度

1,2) 如果 lhsrhs 的大小不同,则为常数时间,否则与 set 的大小成线性关系。
3-7)set 的大小成线性关系。

[编辑] 注解

关系运算符是根据元素类型的 operator< 定义的。

(C++20 前)

关系运算符是根据 synth-three-way 定义的,它尽可能使用 operator<=>,否则使用 operator<

值得注意的是,如果元素本身不提供 operator<=>,但可以隐式转换为三路可比较类型,则将使用该转换而不是 operator<

(C++20 起)

[编辑] 示例

#include <cassert>
#include <compare>
#include <set>
 
int main()
{
    const std::set
        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 &&
    "");
}

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 3431 C++20 operator<=> 未要求 T
满足 three_way_comparable
要求