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


C++ Unordered Set

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 Functions

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

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

Non-member function overloads

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.