版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/qq_41855420,未经博主允许不得转载。 https://blog.csdn.net/qq_41855420/article/details/89765139
C++ set 容器
定义于头文件 <set>之中,
申明为 template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> class set;
std::set 是关联容器,含有 Key 类型对象的已排序集。用比较函数 Compare 进行排序。搜索、移除和插入拥有对数复杂度。 set 通常以红黑树实现。
在每个标准库使用比较 (Compare) 概念的场所,用等价关系确定唯一性。不精确地说,若二个对象 a 与 b 相互间既不比较大于亦不比较小于: !comp(a, b) && !comp(b, a) ,则认为它们等价。
std::set 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。
成员类型
成员类型
定义
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
Allocator::reference (C++11 前) value_type& (C++11 起)
const_reference
Allocator::const_reference (C++11 前) const value_type& (C++11 起)
pointer
Allocator::pointer (C++11 前) std::allocator_traits::pointer (C++11 起)
const_pointer
Allocator::const_pointer (C++11 前) std::allocator_traits::const_pointer (C++11 起)
iterator
常双向迭代器 (LegacyBidirectionalIterator)
const_iterator
常双向迭代器
reverse_iterator
std::reverse_iterator
const_reverse_iterator
std::reverse_iterator<const_iterator>
node_type(C++17 起)
表示容器结点的结点把柄特化
成员函数
(构造函数)
构造 set (公开成员函数)
(析构函数)
析构 set (公开成员函数)
operator=
赋值给容器 (公开成员函数)
get_allocator
返回相关的分配器 (公开成员函数)
迭代器
begin、cbegin 返回指向容器第一个元素的迭代器 (公开成员函数) end、cend 返回指向容器尾端的迭代器 (公开成员函数) rbegin、crbegin 返回指向容器最后元素的逆向迭代器 (公开成员函数) rend、crend 返回指向前端的逆向迭代器 (公开成员函数) 容量
empty
检查容器是否为空 (公开成员函数)
size
返回容纳的元素数 (公开成员函数)
max_size
返回可容纳的最大元素数 (公开成员函数)
修改器
clear
清除内容 (公开成员函数)
insert
插入元素或结点 (C++17 起) (公开成员函数)
emplace(C++11)
原位构造元素 (公开成员函数)
emplace_hint(C++11)
使用提示原位构造元素 (公开成员函数)
erase
擦除元素 (公开成员函数)
swap
交换内容 (公开成员函数)
extract (C++17)
从另一容器释出结点 (公开成员函数)
merge(C++17)
从另一容器接合结点 (公开成员函数)
查找
count
返回匹配特定键的元素数量 (公开成员函数)
find
寻找带有特定键的元素 (公开成员函数)
contains
(C++20) 检查容器是否含有带特定关键的元素 (公开成员函数)
equal_range
返回匹配特定键的元素范围 (公开成员函数)
lower_bound
返回指向首个不小于给定键的元素的迭代器 (公开成员函数)
upper_bound
返回指向首个大于给定键的元素的迭代器 (公开成员函数)观察器
key_comp
返回用于比较键的函数 (公开成员函数)
value_comp
返回用于在value_type类型的对象中比较键的函数。 (公开成员函数)