📅  最后修改于: 2020-10-20 06:16:00             🧑  作者: Mango
C++ STL Set
Set介绍
set是C++ STL(标准模板库)的一部分。集合是存储排序键的关联容器,其中每个键都是唯一的,可以插入或删除但不能更改。
句法
template < class T, // set::key_type/value_type
class Compare = less, // set::key_compare/value_compare
class Alloc = allocator // set::allocator_type
> class set;
参数
T:存储在容器set中的元素类型。
比较:比较类,它接受两个相同类型的布尔变量,并返回一个值。此参数是可选的,二进制谓词较少,是默认值。
Alloc:用于定义存储分配模型的分配器对象的类型。
会员职能
以下是set的所有成员函数的列表:
构造函数/析构函数
Functions |
Description |
(constructor) |
Construct set |
(destructor) |
Set destructor |
operator= |
Copy elements of the set to another set. |
迭代器
Functions |
Description |
Begin |
Returns an iterator pointing to the first element in the set. |
cbegin |
Returns a const iterator pointing to the first element in the set. |
End |
Returns an iterator pointing to the past-end. |
Cend |
Returns a constant iterator pointing to the past-end. |
rbegin |
Returns a reverse iterator pointing to the end. |
Rend |
Returns a reverse iterator pointing to the beginning. |
crbegin |
Returns a constant reverse iterator pointing to the end. |
Crend |
Returns a constant reverse iterator pointing to the beginning. |
容量
Functions |
Description |
empty |
Returns true if set is empty. |
Size |
Returns the number of elements in the set. |
max_size |
Returns the maximum size of the set. |
修饰符
Functions |
Description |
insert |
Insert element in the set. |
Erase |
Erase elements from the set. |
Swap |
Exchange the content of the set. |
Clear |
Delete all the elements of the set. |
emplace |
Construct and insert the new elements into the set. |
emplace_hint |
Construct and insert new elements into the set by hint. |
观察者
Functions |
Description |
key_comp |
Return a copy of key comparison object. |
value_comp |
Return a copy of value comparison object. |
运作方式
Functions |
Description |
Find |
Search for an element with given key. |
count |
Gets the number of elements matching with given key. |
lower_bound |
Returns an iterator to lower bound. |
upper_bound |
Returns an iterator to upper bound. |
equal_range |
Returns the range of elements matches with given key. |
分配者
Functions |
Description |
get_allocator |
Returns an allocator object that is used to construct the set. |
非成员重载函数
Functions |
Description |
operator== |
Checks whether the two sets are equal or not. |
operator!= |
Checks whether the two sets are equal or not. |
operator< |
Checks whether the first set is less than other or not. |
operator<= |
Checks whether the first set is less than or equal to other or not. |
operator> |
Checks whether the first set is greater than other or not. |
operator>= |
Checks whether the first set is greater than equal to other or not. |
swap() |
Exchanges the element of two sets. |