Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


C++ Set

A set is an associative container that stores unique sorted elements. In a set, the value of the element is itself the key, of type T. Each value in a set is unique which can be inserted or deleted in the set but can not be altered.

Sets are typically implemented as Binary Search Tree.

Syntax

template < class T,                        // key_type/value_type
           class Compare = less<T>,        // key_compare/value_compare
           class Alloc = allocator<T>>     // allocator_type
           > class set;

Parameters

T Type of the elements stored in the container.
Compare A binary predicate that takes two elements of set as arguments and returns a bool. Elements are sorted by using this function.
Alloc Type of the allocator object used to define the storage allocation model, default:allocator.

Member Types

Member typesDefinition
key_type T (First template parameter)
value_type T (First template parameter)
key_compare Compare (Second template parameter), default: less<key_type>
value_compare Compare (Second template parameter), default: less<value_type>
allocator_type Alloc (Third template parameter), default: allocator<value_type>
reference value_type&
const_reference const value_type&
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a bidirectional iterator to value_type
const_iterator a bidirectional iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ - Set Functions

The C++ set container has a number of member functions which are listed below:

FunctionsDescription
operator=() Assign content to a set.

Capacity

FunctionsDescription
empty() Checks whether the set is empty or not.
size() Returns the length of the set in terms of bytes.
max_size() Returns the maximum length of the set.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the set.
end() Returns iterator pointing to the past-the-last element of the set.
rbegin() Returns reverse iterator to the last element of the set.
rend() Returns reverse iterator to the element preceding the first element of the set.
cbegin() Returns const_iterator pointing to the first element of the set.
cend() Returns const_iterator pointing to the past-the-last element of the set.
crbegin() Returns const_reverse_iterator to the last element of the set.
crend() Returns const_reverse_iterator to the element preceding the first element of the set.

Modifiers

FunctionsDescription
clear() Clears all elements of the set.
erase() Deletes either a single element or range of elements from a set.
insert() Insert elements in the set.
swap() Exchanges elements between two set.
emplace() Constructs and inserts a unique new element in the set.
emplace_hint() Constructs and inserts a unique new element with hint in the set.

Observers

FunctionsDescription
key_comp() Returns a copy of the comparison object used by the container.
value_comp() Returns a copy of the comparison object used by the container.

Operations

FunctionsDescription
find() Searches the container for a value and returns the iterator to it if found, else returns the iterator to set::end.
count() Returns the number of occurrences of an element in the set container.
lower_bound() Returns an iterator pointing to the first element in the set container which is not considered to go before specified value.
upper_bound() Returns an iterator pointing to the first element in the set container which is considered to go after specified value.
equal_range() Returns the range of elements in the set container that matches with given value.

Non-member function overloads

FunctionsDescription
swap() Exchanges elements between two set.

Allocator

FunctionsDescription
get_allocator() Return a copy of allocator object associated with the set.