命名空间
变体
操作

std::set

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

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class set;
(1)
namespace pmr {

    template<
        class Key,
        class Compare = std::less<Key>
    > using set = std::set<Key, Compare, std::pmr::polymorphic_allocator<Key>>;

}
(2) (C++17 起)

std::set 是一个关联容器,它包含一个已排序的唯一 Key 类型对象的集合。排序使用键比较函数 Compare 完成。搜索、删除和插入操作具有对数复杂度。Set 通常实现为 红黑树

在标准库中使用 Compare 要求的地方,唯一性是通过使用等价关系来确定的。不精确地说,如果两个对象 ab 都不比对方小,则认为它们是等价的:!comp(a, b) && !comp(b, a)

std::set 满足 ContainerAllocatorAwareContainerAssociativeContainerReversibleContainer 的要求。

std::set 的所有成员函数都是 constexpr:可以在常量表达式的评估中创建和使用 std::set 对象。

然而,std::set 对象通常不能是 constexpr,因为任何动态分配的存储都必须在同一常量表达式的评估中释放。

(C++26 起)

目录

[编辑] 模板参数

[编辑] 成员类型

类型 定义
key_type Key[编辑]
value_type Key[编辑]
size_type 无符号整数类型(通常为 std::size_t[编辑]
difference_type 有符号整数类型(通常为 std::ptrdiff_t[编辑]
key_compare Compare[编辑]
value_compare Compare[编辑]
allocator_type Allocator[编辑]
reference value_type&[编辑]
const_reference const value_type&[编辑]
pointer

Allocator::pointer

(C++11 前)

std::allocator_traits<Allocator>::pointer

(C++11 起)
[编辑]
const_pointer

Allocator::const_pointer

(C++11 前)

std::allocator_traits<Allocator>::const_pointer

(C++11 起)
[编辑]
iterator 指向 value_type 的常量 LegacyBidirectionalIterator ConstexprIterator(C++26 起)[编辑]
const_iterator 指向 const value_typeLegacyBidirectionalIteratorConstexprIterator(C++26 起)[编辑]
reverse_iterator std::reverse_iterator<iterator>[编辑]
const_reverse_iterator std::reverse_iterator<const_iterator>[编辑]
node_type (C++17 起) 节点句柄 的特化,表示一个容器节点[编辑]
insert_return_type (C++17起) 描述插入 node_type 结果的类型,一个特化

template<class Iter, class NodeType>
struct /*未指定*/
{
    Iter     position;
    bool     inserted;
    NodeType node;
};

使用模板参数 iteratornode_type 实例化。[编辑]

[编辑] 成员函数

构造 set
(public 成员函数) [编辑]
析构 set
(public 成员函数) [编辑]
将值赋给容器
(public 成员函数) [编辑]
返回关联的分配器
(public 成员函数) [编辑]
迭代器
返回指向起始的迭代器
(public 成员函数) [编辑]
(C++11)
返回指向末尾的迭代器
(public 成员函数) [编辑]
返回指向起始的逆向迭代器
(public 成员函数) [编辑]
(C++11)
返回指向末尾的逆向迭代器
(public 成员函数) [编辑]
容量
检查容器是否为空
(public 成员函数) [编辑]
返回元素数量
(public 成员函数) [编辑]
返回元素的最大可能数量
(public 成员函数) [编辑]
修改器
清除内容
(public 成员函数) [编辑]
插入元素 或节点(C++17 起)
(public 成员函数) [编辑]
插入元素范围
(public 成员函数) [编辑]
(C++11)
就地构造元素
(public 成员函数) [编辑]
使用提示就地构造元素
(public 成员函数) [编辑]
擦除元素
(public 成员函数) [编辑]
交换内容
(public 成员函数) [编辑]
(C++17)
从容器中提取节点
(public 成员函数) [编辑]
(C++17)
从另一个容器拼接节点
(public 成员函数) [编辑]
查找
返回匹配特定键的元素数量
(public 成员函数) [编辑]
查找具有特定键的元素
(public 成员函数) [编辑]
(C++20)
检查容器是否包含具有特定键的元素
(public 成员函数) [编辑]
返回与特定键匹配的元素范围
(public 成员函数) [编辑]
返回指向第一个不小于给定键的元素的迭代器
(public 成员函数) [编辑]
返回指向第一个大于给定键的元素的迭代器
(public 成员函数) [编辑]
观察器
返回比较键的函数
(public 成员函数) [编辑]
返回比较 value_type 类型对象中的键的函数
(public 成员函数) [编辑]

[编辑] 非成员函数

(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20)
按字典顺序比较两个 set 的值
(函数模板) [编辑]
特化 std::swap 算法
(函数模板) [编辑]
擦除所有满足特定标准的元素
(函数模板) [编辑]

推导指引

(C++17 起)

[编辑] 注意

成员类型 iteratorconst_iterator 可以是相同类型的别名。这意味着定义一对使用这两种类型作为参数类型的函数重载可能会违反一条定义规则。由于 iterator 可以转换为 const_iterator,因此使用 const_iterator 作为参数类型的单个函数将起作用。

特性测试 标准 特性
__cpp_lib_containers_ranges 202202L (C++23) 容器的范围构造和插入
__cpp_lib_constexpr_containers 202502L (C++26) constexpr std::set

[编辑] 示例

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <set>
#include <string_view>
 
template<typename T>
std::ostream& operator<<(std::ostream& out, const std::set<T>& set)
{
    if (set.empty())
        return out << "{}";
    out << "{ " << *set.begin();
    std::for_each(std::next(set.begin()), set.end(), [&out](const T& element)
    {
        out << ", " << element;
    });
    return out << " }";
}
 
int main()
{
    std::set<int> set{1, 5, 3};
    std::cout << set << '\n';
 
    set.insert(2);
    std::cout << set << '\n';
 
    set.erase(1);
    std::cout << set << "\n\n";
 
    std::set<int> keys{3, 4};
    for (int key : keys)
    {
        if (set.contains(key))
            std::cout << set << " does contain " << key << '\n';
        else
            std::cout << set << " doesn't contain " << key << '\n';
    }
    std::cout << '\n';
 
    std::string_view word = "element";
    std::set<char> characters(word.begin(), word.end());
    std::cout << "There are " << characters.size() << " unique characters in "
              << std::quoted(word) << ":\n" << characters << '\n';
}

输出

{ 1, 3, 5 }
{ 1, 2, 3, 5 }
{ 2, 3, 5 }
 
{ 2, 3, 5 } does contain 3
{ 2, 3, 5 } doesn't contain 4
 
There are 5 unique characters in "element":
{ e, l, m, n, t }

[编辑] 缺陷报告

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

缺陷报告 应用于 发布时的行为 正确的行为
LWG 103 C++98 迭代器允许修改键 迭代器设为常量
LWG 230 C++98 不要求 Key可复制构造的 (CopyConstructible)
(类型为 Key 的键可能无法构造)
Key 也要求
可复制构造 (CopyConstructible)

[编辑] 参见

键的集合,按键排序
(类模板) [编辑]
由键哈希的唯一键集合
(类模板) [编辑]
(C++23)
适配容器以提供唯一键的集合,按键排序
(类模板) [编辑]