C++ STL Set (set)
Introduction to set
setisC ++ Part of the STL (Standard Template Library)A set is an associative container that stores sorted keys, where each key is unique and can be inserted or deleted but not modified.
Syntax
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
Parameter
TThe type of elements stored in the container set.
CompareA comparison class that accepts two parameters of the same bool type and returns a value. This parameter is optional, and the binary predicate less <T> is the default value.
AllocThe type of allocator object used to define the storage allocation model.
Member function
The following is a list of all member functions of set:
Constructor/Destructor
Iterator
Function | Description |
---|
begin | Returns an iterator pointing to the first element of the set. |
cbegin | Returns a const iterator pointing to the first element of the set. |
end | Returns an iterator pointing to the end of the set. |
cend | Returns a constant iterator pointing to the end. |
rbegin | Returns a reverse iterator pointing to the end. |
rend | Returns a reverse iterator pointing to the start. |
crbegin | Returns a constant reverse iterator pointing to the end. |
crend | Returns a constant reverse iterator pointing to the start. |
Capacity
Function | Description |
---|
empty | Returns true if the set is empty. |
size | Returns the number of elements in the set. |
max_size | Returns the maximum size of the set. |
Modifier
Function | Description |
---|
insert | Inserts an element into the set. |
erase | Erases elements from the set. |
swap | Swaps the content of the set. |
clear | Deletes all elements in the set. |
emplace | Constructs a new element and inserts it into the set. |
emplace_hint | Constructs a new element by hint and inserts it into the set. |
Observation comparison
Function | Description |
---|
key_comp | Returns a function used for value comparison between elements. |
value_comp | Returns a function used to compare values between elements. |
Operation search
Function | Description |
---|
find | Searches for an element with a given key. |
count | Gains the number of elements that match a given key. |
lower_bound | Returns an iterator pointing to the first element that is greater than (or equal to) a given value. |
upper_bound | Returns an iterator to the first element that is greater than a given value. |
equal_range | Return two iterators equal to the given value in the set. |
Allocator
Function | Description |
---|
get_allocator | Return the allocator object used to construct the set. |
Non-member Overloaded Functions
Function | Description |
---|
operator== | Check if two groups are equal. |
operator!= | Check if two groups are equal. |
operator< | Check if the first group is less than other groups. |
operator<= | Check if the first group is less than or equal to others. |
operator> | Check if the first set is greater than other sets. |
operator>= | Check if the first set is greater than other sets. |
swap() | Swap elements of two groups. |