In C++, unordered associative containers or unordered associative collections are a group of class templates in the C++ Standard Library that implement hash table variants. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. Like all other standard library components, they reside in namespace <code>std</code>.
The following containers are defined in the current revision of the C++ standard:
Each of these containers differ only on constraints placed on their elements.
<code>std::unordered_set</code> and <code>std::unordered_multiset</code> are declared in header <code><unordered_set></code>, while <code>std::unordered_map</code> and <code>std::unordered_multimap</code> are declared in header <code><unordered_map></code>.
There are also versions of these collections in namespace <code>std::pmr</code> (for polymorphic memory resources). These versions specify the optional template parameter <code>Allocator</code> as <code>std::pmr::polymorphic_allocator</code>.
The unordered associative containers are similar to the associative containers in the C++ Standard Library but have different constraints. As their name implies, the elements in the unordered associative containers are not ordered. This is due to the use of hashing to store objects. The containers can still be iterated through like a regular associative container.
<code>std::unordered_map</code> and <code>std::unordered_set</code> are essentially (respectively) equivalent to <code>java.util.HashMap</code> and <code>java.util.HashSet</code> from Java, <code>System.Collections.Generic.Dictionary</code> and <code>System.Collections.Generic.HashSet</code> from .NET, or <code>std::collections::HashMap</code> and <code>std::collections::HashSet</code> from Rust.
The first widely used implementation of hash tables in the C++ language was <code>hash_map</code>, <code>hash_set</code>, <code>hash_multimap</code>, <code>hash_multiset</code> class templates of the Silicon Graphics (SGI) Standard Template Library (STL). Due to their usefulness, they were later included in several other implementations of the C++ Standard Library (e.g., the GNU Compiler Collection's (GCC) libstdc++ and the Visual C++ (MSVC) standard library).
The <code>hash_*</code> class templates were proposed into C++ Technical Report 1 (C++ TR1) and were accepted under names <code>unordered_*</code>. Later, they were incorporated into the C++11 revision of the C++ standard. An implementation is also available in the Boost C++ Libraries as <code><boost/unordered_map.hpp></code>.
The containers are defined in headers named after the names of the containers, e.g., <code>unordered_set</code> is defined in header <code><unordered_set></code>. All containers satisfy the requirements of the Container concept, which means they have <code>begin()</code>, <code>end()</code>, <code>size()</code>, <code>max_size()</code>, <code>empty()</code>, and <code>swap()</code> methods.
To use custom objects in <code>std::unordered_map</code>, a custom hasher must be defined. This function takes a const reference to the custom type and returns a <code>size_t</code>.
The user defined function can be used as is in <code>std::unordered_map</code>, by passing it as a template parameter
Or can be set as the default hash function by specializing <code>std::hash</code>: