Cpp Standard Library 简明教程
C++ Library - <set>
Introduction
set 是一种关联容器,它包含了一个类型为 Key 的唯一对象的有序集合。每个元素只能出现一次,因此不允许重复元素。
A set is an Associative container which contains a sorted set of unique objects of type Key. Each element may occur only once, so duplicates are not allowed.
关联容器有四种类型:集合、多重集合、映射和多重映射。
There are four kind of Associative containers: set, multiset, map and multimap.
一旦添加到容器中,集合中元素的值就不能被修改,即,元素总是 const。但是,它们可以从容器中插入或删除。
The value of the elements in a set cannot be modified once in the container, i.e., the elements are always const. But they can be inserted or removed from the container.
set 容器通常较 unordered_set 容器访问单个元素慢,但允许直接根据元素顺序迭代子集。
set containers are generally slower than unordered_set containers in accessing individual elements by their key, but they allow the direct iteration on subsets based on their order.
Definition
以下是 <set> 标头文件中 std::set 的定义
Below is definition of std::set from <set> header file
template <
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class set;
Parameters
-
Key − Type of the element contained. Key may be substituted by any other data type including user-defined type.
Member types
以下成员类型可作为参数或返回类型,由成员函数使用。
Following member types can be used as parameters or return type by member functions.
Sr.No. |
Member types |
Definition |
1 |
key_type |
Key |
2 |
value_type |
Key |
3 |
reference |
Allocator::reference value_type& |
4 |
const_reference |
Allocator::const_reference const value_type& |
5 |
pointer |
Allocator::pointer std::allocator_traits<Allocator>::pointer |
6 |
const_pointer |
Allocator::const_pointerstd::allocator_traits<Allocator>::const_pointer |
7 |
iterator |
BidirectionalIterator |
8 |
const_iterator |
constant BidirectionalIterator |
9 |
reverse_iterator |
std::reverse_iterator <iterator> |
10 |
const_reverse_iterator |
std::reverse_iterator <const_iterator> |
11 |
size_type |
Unsigned Integer Type (std::size_t) |
12 |
difference_type |
Signed Integer Type (std::ptrdiff_t) |
13 |
key_compare |
Compare |
14 |
value_compare |
Compare |
15 |
allocator_type |
Allocator |
Functions from <set>
以下是 <set> 标头文件中所有方法的列表。
Below is list of all methods from <set> header.
MEMBER FUNCTIONS
DEFAULT MEMBER FUNCTIONS
DEFAULT MEMBER FUNCTIONS
Sr.No. |
Method & Description |
1 |
Default constructorConstructs the set container. |
2 |
Range constructorConstructs the set container with contents of the range. |
3 |
Copy constructorConstructs the set container with the copy of other set. |
4 |
Move constructorConstructs the set container with the contents of other set using move semantics. |
5 |
Initializer-list constructorConstructs the set container with contents of the inializer list. |
6 |
(destructor)Destructs the set container. |
7 |
[role="bare"]../cpp_standard_library/cpp_set_operator=.htmlAssigns values to the set container. |
ITERATORS
Sr.No. |
Method & Description |
1 |
set::beginReturns the iterator to beginning. |
2 |
set::cbeginReturns the const iterator to beginning. |
3 |
set::endReturns the iterator to end. |
4 |
set::cendReturns the const iterator to end. |
5 |
set::rbeginReturns the reverse iterator to reverse beginning. |
6 |
set::crbeginReturn const reverse iterator to reverse beginning. |
7 |
set::rendReturns the reverse iterator to reverse end. |
8 |
set::crendReturns the const reverse iterator to reverse end. |
CAPACITY
Sr.No. |
Method & Description |
1 |
set::emptyReturns wheteher the set container is empty. |
2 |
set::sizeReturns the number of elements in the set container. |
3 |
set::max_sizeReturns the maximum number of elements that the set container can hold. |
MODIFIERS
Sr.No. |
Method & Description |
1 |
set::clearRemoves all elements from the set container. |
2 |
set::insertInserts new element in the set container. |
3 |
set::emplaceInserts new element in the set, if its unique. |
4 |
set::emplace_hintInserts new element in the set, if its unique, with a hint on the inserting position. |
5 |
set::eraseRemoves either a single element or a range of elements from the set container. |
6 |
set::swapExchanges the content of the container by the content of another set container of the same type. |
LOOKUP
Sr.No. |
Method & Description |
1 |
set::countReturns the number of elements with matching value in the set container. |
2 |
set::findSearches the set container for value and returns an iterator to it if found, else returns an iterator to set::end. |
3 |
set::lower_boundReturns an iterator pointing to the first element in the set container which is not considered to go before value. |
4 |
set::upper_boundReturns an iterator pointing to the first element in the set container which is considered to go after value. |
5 |
set::equal_rangeReturns the bounds of a range that includes all the elements in the set container that are equivalent to value. |
OBSERVERS
Sr.No. |
Method & Description |
1 |
set::key_compReturns a copy of the comparison object used by the set container. |
2 |
set::value_compReturns a copy of the comparison object used by the set container. |
ALLOCATOR
Sr.No. |
Method & Description |
1 |
set::get_allocatorReturns a copy of the allocator object associated with the set container. |