class template
<unordered_set>

std::unordered_set

template < class Key,                        // unordered_set::key_type/value_type
           class Hash = hash<Key>,           // unordered_set::hasher
           class Pred = equal_to<Key>,       // unordered_set::key_equal
           class Alloc = allocator<Key>      // unordered_set::allocator_type
           > class unordered_set;
Unordered Set
Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.

In an unordered_set, the value of an element is at the same time its key, that identifies it uniquely. Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container - they can be inserted and removed, though.

Internally, the elements in the unordered_set are not sorted in any particular order, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values (with a constant average time complexity on average).

unordered_set containers are faster than set containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

Iterators in the container are at least forward iterators.

Container properties

Associative
Elements in associative containers are referenced by their key and not by their absolute position in the container.
Unordered
Unordered containers organize their elements using hash tables that allow for fast access to elements by their key.
Set
The value of an element is also the key used to identify it.
Unique keys
No two elements in the container can have equivalent keys.
Allocator-aware
The container uses an allocator object to dynamically handle its storage needs.

Template parameters

Key
Type of the elements. Each element in an unordered_set is also uniquely identified by this value.
Aliased as member types unordered_set::key_type and unordered_set::value_type.
Hash
A unary function object type that takes an object of the same type as the elements as argument and returns a unique value of type size_t based on it. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to hash<Key>, which returns a hash value with a probability of collision approaching 1.0/std::numeric_limits<size_t>::max().
The unordered_set object uses the hash values returned by this function to organize its elements internally, speeding up the process of locating individual elements.
Aliased as member type unordered_set::hasher.
Pred
A binary predicate that takes two arguments of the same type as the elements and returns a bool. The expression pred(a,b), where pred is an object of this type and a and b are key values, shall return true if a is to be considered equivalent to b. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to equal_to<Key>, which returns the same as applying the equal-to operator (a==b).
The unordered_set object uses this expression to determine whether two element keys are equivalent. No two elements in an unordered_set container can have keys that yield true using this predicate.
Aliased as member type unordered_set::key_equal.
Alloc
Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type unordered_set::allocator_type.

In the reference for the unordered_set member functions, these same names (Key, Hash, Pred and Alloc) are assumed for the template parameters.

Member types

The following aliases are member types of unordered_set. They are widely used as parameter and return types by member functions:

member typedefinitionnotes
key_typethe first template parameter (Key)
value_typethe first template parameter (Key)The same as key_type
hasherthe second template parameter (Hash)defaults to: hash<key_type>
key_equalthe third template parameter (Pred)defaults to: equal_to<key_type>
allocator_typethe fourth template parameter (Alloc)defaults to: allocator<value_type>
referenceAlloc::reference
const_referenceAlloc::const_reference
pointerAlloc::pointerfor the default allocator: value_type*
const_pointerAlloc::const_pointerfor the default allocator: const value_type*
iteratora forward iterator to const value_type* convertible to const_iterator
const_iteratora forward iterator to const value_type*
local_iteratora forward iterator to const value_type* convertible to const_local_iterator
const_local_iteratora forward iterator to const value_type*
size_typean unsigned integral typeusually the same as size_t
difference_typea signed integral typeusually the same as ptrdiff_t
member typedefinitionnotes
key_typethe first template parameter (Key)
value_typethe first template parameter (Key)The same as key_type
hasherthe second template parameter (Hash)defaults to: hash<key_type>
key_equalthe third template parameter (Pred)defaults to: equal_to<key_type>
allocator_typethe fourth template parameter (Alloc)defaults to: allocator<value_type>
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<Alloc>::pointerfor the default allocator: value_type*
const_pointerallocator_traits<Alloc>::const_pointerfor the default allocator: const value_type*
iteratora forward iterator to const value_type* convertible to const_iterator
const_iteratora forward iterator to const value_type*
local_iteratora forward iterator to const value_type* convertible to const_local_iterator
const_local_iteratora forward iterator to const value_type*
size_typean unsigned integral typeusually the same as size_t
difference_typea signed integral typeusually the same as ptrdiff_t
*Note: All iterators in an unordered_set point to const elements. Whether the const_ member type is the same type as its non-const_ counterpart depends on the particular library implementation, but programs should not rely on them being different to overload functions: const_iterator is more generic, since iterator is always convertible to it.
The same applies to local_ and non-local_ iterator types: they may either be the same type or not, but a program should not rely on them being different.

Member functions


Capacity


Iterators


Element lookup


Modifiers


Buckets


Hash policy


Observers


Non-member function overloads