C++ Standard Library C++ STL Library

C++ - <set>



The C++ <set> header file defines the set and multiset container classes:

set class

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 - Member Functions

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

FunctionsDescription
set() Construct a set object.
~set() Destroys container by deallocating container memory.
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.

Allocator

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

C++ set - Non-member Functions

FunctionsDescription
swap() Exchanges elements between two set.

multiset class

A multiset is an associative container that stores sorted elements. Unlike set, in a multiset multiple elements can have same values and the value of the element is itself the key, of type T. In a multiset, values can be inserted or deleted but can not be altered.

Multisets 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 multiset;

Parameters

T Type of the elements stored in the container.
Compare A binary predicate that takes two elements of multiset 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++ multiset - Member Functions

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

FunctionsDescription
multiset() Construct a multiset object.
~multiset() Destroys container by deallocating container memory.
operator=() Assign content to a multiset.

Capacity

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

Iterators

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

Modifiers

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

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 multiset::end.
count() Returns the number of occurrences of an element in the multiset container.
lower_bound() Returns an iterator pointing to the first element in the multiset container which is not considered to go before specified value.
upper_bound() Returns an iterator pointing to the first element in the multiset container which is considered to go after specified value.
equal_range() Returns the range of elements in the multiset container that matches with given value.

Allocator

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

C++ multiset - Non-member Functions

FunctionsDescription
swap() Exchanges elements between two Multiset.