English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

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

FunctionDescription
(constructor)Construct set
(destructor)set destructor
operator=Copies the elements of the set to another set.

Iterator

FunctionDescription
beginReturns an iterator pointing to the first element of the set.
cbeginReturns a const iterator pointing to the first element of the set.
endReturns an iterator pointing to the end of the set.
cendReturns a constant iterator pointing to the end.
rbeginReturns a reverse iterator pointing to the end.
rendReturns a reverse iterator pointing to the start.
crbeginReturns a constant reverse iterator pointing to the end.
crendReturns a constant reverse iterator pointing to the start.

Capacity

FunctionDescription
emptyReturns true if the set is empty.
sizeReturns the number of elements in the set.
max_sizeReturns the maximum size of the set.

Modifier

FunctionDescription
insertInserts an element into the set.
eraseErases elements from the set.
swapSwaps the content of the set.
clearDeletes all elements in the set.
emplaceConstructs a new element and inserts it into the set.
emplace_hintConstructs a new element by hint and inserts it into the set.

Observation comparison

FunctionDescription
key_compReturns a function used for value comparison between elements.
value_compReturns a function used to compare values between elements.

Operation search

FunctionDescription
findSearches for an element with a given key.
countGains the number of elements that match a given key.
lower_boundReturns an iterator pointing to the first element that is greater than (or equal to) a given value.
upper_boundReturns an iterator to the first element that is greater than a given value.
equal_rangeReturn two iterators equal to the given value in the set.

Allocator

FunctionDescription
get_allocatorReturn the allocator object used to construct the set.

Non-member Overloaded Functions

FunctionDescription
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.