Home >>C++ Standard Template Library Tutorial(STL) >C++ Set

C++ Set

Set in C++

The parts of the STL (Standard Template Library) that are basically associative containers which are used to store the sorted key are known as the STL set in C++. Generally in these keys, each key is unique and can either be inserted or deleted but in any case it cannot be altered.

Syntax

Here is the syntax of Set:

template < class S, 
           class Compare = less<S>,        
           class Alloc = allocator<S>	      
           > class set;  

C++ STL Set Member Functions

Here are the member function of the STL Set:

Functions Description
constructor This is known as the Construct set
destructor This is known as the Set destructor
operator= This member generally copy elements of the set to another set.

Iterators

In order to specify a particular range of things on which we have to operate, Iterators in STL proves to be very useful in that. Iterators in STL generally delivers the means by which the data stored in the container classes like vector, map, list etc. can be accessed.
Here are some of the functions of Iterators in STL SET library depicted below:

Functions Description
Begin() This member function is used to return an iterator that is pointing to the first element in the set.
cbegin() This member function is used to return a const iterator that is pointing to the first element in the set.
End() This member function is used to return an iterator that is pointing to the past-end.
Cend() This member function is used to return a constant iterator that is pointing to the past-end.
rbegin() This member function is used to return a reverse iterator that is pointing to the end.
Rend() This member function is used to return a reverse iterator that is pointing to the beginning.
crbegin() This member function is used to returns a constant reverse iterator that is pointing to the end.
Crend() This member function is used to return a constant reverse iterator that is pointing to the beginning.

Capacity

The Capacity in STL generally returns the size of space that has been allocated to the vector in terms of elements. It is not mandatory that the capacity should be equal to the vector size. There is no limit in the terms of size that is implemented on the vector by the capacity however; this capacity is automatically expanded by the container whenever this capacity gets exhausted.

Here are some of the functions of the capacity in STL SET:

Functions Description
empty() This function is used to return true if the set is empty.
size() This function is used to return the number of elements in the set.
max_size() This function is used to return the maximum size of the set.

Modifiers

As the name suggests, these sets are used in order to make any update or changes in the program with the help of functions that are provided by these modifiers in STL.

Here are some of the functions that are delivered by the modifiers in STL SET:

Functions Description
insert() This function is used to insert element in the set.
Erase() This function is used to erase elements from the set.
Swap() This function is used to exchange the content of the set.
Clear() This function is used to delete all the elements of the set.
emplace() This function is used to construct and insert the new elements into the set.
emplace_hint() This function is used to construct and insert new elements into the set by hint.

Observers

Observers in STL SET are the member functions that are generally used when there is a need to observe the compared object.

Here are some of the functions of operators in STL SET that are described in brief:

Functions Description
key_comp() This function is used to return a copy of key comparison object.
value_comp() This function is used to return a copy of value comparison object.

Operations

Operations in STL SET are the member functions that are generally used to perform the common operations like searching for an element and counting the number of element and much more.

Here is the list of the functions that are delivered by the Operations in STL SET:

Functions Description
Find() This function is used to search for an element with given key.
count() This function is used to get the number of elements that are matching with given key.
lower_bound() This function is used to return an iterator to the lower bound.
upper_bound() This function is used to returns an iterator to the upper bound.
equal_range() This function is used to returns the range of the elements that matches with given key.
get_allocator() This function is generally used to return an allocator object that is used to construct the set.

Non-Member Overloaded Functions

Non-Member Overloaded Functions in STL SET are basically the functions that cannot be overloaded in the STL SET. These functions are generally used to perform a check in various operations.

Here are the list of various functions that are delivered by the Non-Member Overloaded Functions in STL SET:

Functions Description
operator== This function mainly checks whether the two sets are equal or not.
operator!= This function is generally used to check whether the two sets are equal or not.
operator< This function is generally used to check whether the first set is less than other or not.
operator<= This function is generally used to check whether the first set is less than or equal to other or not.
operator> This function is generally used to check whether the first set is greater than other or not.
operator>= This function is generally used to check whether the first set is greater than equal to other or not.