C++ Standard Library C++ STL Library

C++ - <unordered_set>



The C++ <unordered_set> header file defines the unordered_set and unordered_multiset container classes:

unordered_set class

An unordered set is an associative container that stores unique elements in no particular order, which allows fast retrieval of an element based on its value. In a unordered_set, the value of the element is itself the key, of type T. Each value in a unordered_set is unique which can be inserted or deleted in the unordered_set but can not be altered.

Syntax

template < class Key,                        // key_type/value_type
           class Hash = hash<Key>,           // hasher
           class Pred = equal_to<Key>,       // key_equal
           class Alloc = allocator<Key>>     // allocator_type
           > class unordered_set;

Parameters

Key Type of the elements stored in the container.
Hash A unary function that takes two elements of unordered_set as arguments and returns a unique value of type size_t based on it.
Pred A binary predicate that takes two elements of unordered_set as arguments and returns a bool. It is used to determine whether two elements are equivalent or not.
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)
hasher Hash (Second template parameter), default: hash<key_type>
key_equal Pred (Third template parameter), default: equal_to<key_type>
allocator_type Alloc (Fourth template parameter), default: allocator<value_type>
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a forward iterator to const value_type, convertible to const_iterator
const_iterator a forward iterator to const value_type
local_iterator a forward iterator to const value_type, convertible to const_local_iterator
const_local_iterator a forward iterator to const value_type
difference_type ptrdiff_t
size_type size_t

C++ unordered_set - Member Functions

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

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

Capacity

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

Element lookup

FunctionsDescription
find() Searches the container for a value and returns the iterator to it if found, else returns the iterator to unordered_set::end.
count() Returns the number of occurrences of an element in the unordered_set container.
equal_range() Returns the range of elements in the unordered_set container that matches with given value.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the unordered_set.
end() Returns iterator pointing to the past-the-last element of the unordered_set.
cbegin() Returns const_iterator pointing to the first element of the unordered_set.
cend() Returns const_iterator pointing to the past-the-last element of the unordered_set.

Modifiers

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

Buckets

FunctionsDescription
bucket_count() Returns the number of buckets in the unordered_set.
max_bucket_count() Returns the maximum number of buckets that the unordered_set can have.
bucket_size() Returns the number of elements in the specified bucket of the unordered_set.
bucket() Returns the bucket number of the specified element of the unordered_set.

Hash policy

FunctionsDescription
load_factor() Returns current load factor in the unordered_set.
max_load_factor() Returns or sets the maximum load factor in the unordered_set.
rehash() Sets number of buckets in the unordered_set.
reserve() Requests a capacity change in the unordered_set.

Observers

FunctionsDescription
hash_function() Get the hash function object used by unordered_set container.
key_eq() Returns the function which compares keys for equality.
get_allocator() Return a copy of allocator object associated with the unordered_set.

C++ unordered_set - Non-member Functions

FunctionsDescription
operator == Checks whether two unordered_sets are equal or not.
operator != Checks whether two unordered_sets are unequal or not.
swap() Exchanges elements between two unordered_sets.

unordered_multiset class

A unordered multiset is an associative container that stores elements in no particular order, which allows fast retrieval of an element based on its value. Unlike unordered_set or set, in a unordered_multiset multiple elements can have same values and the value of the element is itself the key, of type T. In a unordered_multiset, values can be inserted or deleted but can not be altered.

Syntax

template < class Key,                        // key_type/value_type
           class Hash = hash<Key>,           // hasher
           class Pred = equal_to<Key>,       // key_equal
           class Alloc = allocator<Key>>     // allocator_type
           > class unordered_multiset;

Parameters

Key Type of the elements stored in the container.
Hash A unary function that takes two elements of unordered_multiset as arguments and returns a unique value of type size_t based on it.
Pred A binary predicate that takes two elements of unordered_multiset as arguments and returns a bool. It is used to determine whether two elements are equivalent or not.
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)
hasher Hash (Second template parameter), default: hash<key_type>
key_equal Pred (Third template parameter), default: equal_to<key_type>
allocator_type Alloc (Fourth template parameter), default: allocator<value_type>
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a forward iterator to const value_type, convertible to const_iterator
const_iterator a forward iterator to const value_type
local_iterator a forward iterator to const value_type, convertible to const_local_iterator
const_local_iterator a forward iterator to const value_type
difference_type ptrdiff_t
size_type size_t

C++ unordered_multiset - Member Functions

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

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

Capacity

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

Element lookup

FunctionsDescription
find() Searches the container for a value and returns the iterator to it if found, else returns the iterator to unordered_multiset::end.
count() Returns the number of occurrences of an element in the unordered_multiset container.
equal_range() Returns the range of elements in the unordered_multiset container that matches with given value.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the unordered_multiset.
end() Returns iterator pointing to the past-the-last element of the unordered_multiset.
cbegin() Returns const_iterator pointing to the first element of the unordered_multiset.
cend() Returns const_iterator pointing to the past-the-last element of the unordered_multiset.

Modifiers

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

Buckets

FunctionsDescription
bucket_count() Returns the number of buckets in the unordered_multiset.
max_bucket_count() Returns the maximum number of buckets that the unordered_multiset can have.
bucket_size() Returns the number of elements in the specified bucket of the unordered_multiset.
bucket() Returns the bucket number of the specified element of the unordered_multiset.

Hash policy

FunctionsDescription
load_factor() Returns current load factor in the unordered_multiset.
max_load_factor() Returns or sets the maximum load factor in the unordered_multiset.
rehash() Sets number of buckets in the unordered_multiset.
reserve() Requests a capacity change in the unordered_multiset.

Observers

FunctionsDescription
hash_function() Get the hash function object used by unordered_multiset container.
key_eq() Returns the function which compares keys for equality.
get_allocator() Return a copy of allocator object associated with the unordered_multiset.

C++ unordered_multiset - Non-member Functions

FunctionsDescription
operator == Checks whether two unordered_multisets are equal or not.
operator != Checks whether two unordered_multisets are unequal or not.
swap() Exchanges elements between two unordered_multisets.