Namespaces
Variants

std:: set

From cppreference.net
定义于头文件 <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 实现。搜索、删除和插入操作具有对数复杂度。集合通常实现为 红黑树

凡是标准库使用 Compare 要求的地方,唯一性都是通过等价关系来确定的。不严格地说,两个对象 a b 被认为是等价的,当且仅当两者互不小于对方: ! comp ( a, b ) && ! comp ( b, a )

std::set 满足 Container AllocatorAwareContainer AssociativeContainer ReversibleContainer 的要求。

std::set 的所有成员函数均为 constexpr :可以在常量表达式求值过程中创建和使用 std::set 对象。

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

(since 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_type LegacyBidirectionalIterator ConstexprIterator (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 ;
} ;

使用模板参数 iterator node_type 实例化。

成员函数

构造 set 对象
(公开成员函数)
析构 set 对象
(公开成员函数)
赋值给容器
(公开成员函数)
返回关联的分配器
(公开成员函数)
迭代器
返回指向起始位置的迭代器
(公开成员函数)
(C++11)
返回指向末尾位置的迭代器
(公开成员函数)
返回指向起始位置的反向迭代器
(公开成员函数)
(C++11)
返回指向末尾位置的反向迭代器
(公开成员函数)
容量
检查容器是否为空
(公开成员函数)
返回元素数量
(公开成员函数)
返回可容纳的最大元素数量
(公开成员函数)
修改器
清空内容
(公开成员函数)
插入元素 或节点 (since C++17)
(公开成员函数)
插入元素范围
(公开成员函数)
(C++11)
原位构造元素
(公开成员函数)
使用提示原位构造元素
(公开成员函数)
擦除元素
(公开成员函数)
交换内容
(公开成员函数)
(C++17)
从容器提取节点
(公开成员函数)
(C++17)
从另一容器接合节点
(公开成员函数)
查找
返回匹配特定键的元素数量
(公开成员函数)
<a href="set

非成员函数

(C++20 中移除) (C++20 中移除) (C++20 中移除) (C++20 中移除) (C++20 中移除) (C++20)
按字典序比较两个 set 的值
(函数模板)
特化 std::swap 算法
(函数模板)
擦除所有满足特定条件的元素
(函数模板)

推导指引

(C++17 起)

注释

成员类型 iterator const_iterator 可能是同一类型的别名。这意味着使用这两种类型作为参数类型来定义一对函数重载可能会违反 单一定义规则 。由于 iterator 可转换为 const_iterator ,使用以 const_iterator 作为参数类型的单一函数即可实现相同功能。

功能测试 标准 功能
__cpp_lib_containers_ranges 202202L (C++23) 容器的范围构造与插入
__cpp_lib_constexpr_set 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)
适配容器以提供按键排序的唯一键集合
(类模板)