HOME

TheInfoList



OR:

In the programming language C++, unordered associative containers 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. The following containers are defined in the current revision of the C++ standard: unordered_set, unordered_map, unordered_multiset, unordered_multimap. Each of these containers differ only on constraints placed on their elements. The unordered associative containers are similar to the
associative containers In computing, associative containers refer to a group of class templates in the standard library of the C++ programming language that implement ordered associative arrays. Being templates, they can be used to store arbitrary elements, such as ...
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 Iteration is the repetition of a process in order to generate a (possibly unbounded) sequence of outcomes. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next iteration. ...
through like a regular associative container.


History

The first widely used implementation of hash tables in the C++ language was hash_map, hash_set, hash_multimap, hash_multiset 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 hash_* class templates were proposed into C++ Technical Report 1 (C++ TR1) 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 th
Container
concept, which means they have begin(), end(), size(), max_size(), empty(), and swap() methods.


Usage example

#include #include #include int main()


Custom hash functions

To use custom objects in std::unordered_map, a custom hash function must be defined. This function takes a const reference to the custom type and returns a size_t #include struct X; struct hash_X; The user defined function can be used as is in std::unordered_map, by passing it as a template parameter std::unordered_map my_map; Or can be set as the default hash function by specializing the std::hash function namespace std //... std::unordered_map my_map;


References

{{use dmy dates, date=January 2012 Articles with example C++ code C++ Standard Library