Unordered associative containers (C++)
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
std.The following containers are defined in the current revision of the C++ standard:
std::unordered_set, std::unordered_map, std::unordered_multiset, std::unordered_multimap. Each of these containers differ only on constraints placed on their elements.
std::unorderd_set and std::unordered_multiset are declared in header , while std::unordered_map and std::unordered_multimap are declared in header .There are also versions of these collections in namespace
std::pmr. These versions specify the optional template parameter Allocator as std::pmr::polymorphic_allocator.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.
std::unordered_map and std::unordered_set are essentially equivalent to java.util.HashMap and java.util.HashSet from Java, or std::collections::HashMap and std::collections::HashSet from Rust.History
The first widely used implementation of hash tables in the C++ language washash_map, hash_set, hash_multimap, hash_multiset class templates of the Silicon Graphics Standard Template Library. Due to their usefulness, they were later included in several other implementations of the C++ Standard Library libstdc++ and the Visual C++.The
hash_* class templates were proposed into C++ Technical Report 1 and were accepted under names unordered_*. Later, they were incorporated into the C++11 revision of the C++ standard. An implementation is also available in the Boost C++ Libraries as .Overview of functions
The containers are defined in headers named after the names of the containers, e.g.,unordered_set is defined in header . All containers satisfy the requirements of the concept, which means they have begin, end, size, max_size, empty, and swap methods.Usage example
import std;
using std::string;
using std::unordered_map;
const unordered_map
int main
Custom hash functions
To use custom objects instd::unordered_map, a custom hasher must be defined. This function takes a const reference to the custom type and returns a size_t.import std;
using std::hash;
struct Vector3 ;
struct HashVector3 ;
The user defined function can be used as is in
std::unordered_map, by passing it as a template parameter unordered_map
Or can be set as the default hash function by specializing
std::hash:namespace std
//...
unordered_map