Unordered associative containers (C++)
C++ Standard Library |
---|
Containers |
C standard library |
unordered_map
is a class template representing a hash table in the C++ Technical Report 1 (TR1)[1] and the C++11 standard[2]. It is similar to the hash_map
class template of the original STL[3] which was also included in several implementations of the C++ Standard Library (e.g. in the GCC's libstdc++[4] or with MSVC).
It is similar to the map
class in the C++ standard library but has different constraints. As its name implies, unlike the map
class, the elements of an unordered_map
are not ordered. This is due to the use of hashing to store objects. unordered_map
can still be iterated through like a regular map.
In the TR1, unordered_map
is available from the <tr1/unordered_map>
header file as std::tr1::unordered_map
. In C++11, it is available from the <unordered_map>
header file as std::unordered_map
. An implementation is also available in the Boost C++ Libraries as <boost/unordered_map.hpp>
[5].
It is closely related to unordered_multimap
(available in the same header file), which is an implementation of multimap using hashing. It is also similar to unordered_set
and unordered_multiset
, implementations of the set and multiset containers respectively and available in the <tr1/unordered_set>
or <unordered_set>
headers.
Design
![]() | This section needs expansion. You can help by adding to it. |
Overview of functions
Overview of functions
unordered_map::unordered_map
(constructor) - Constructs the unordered map from variety of sources.unordered_map::~unordered_map
(destructor) - Destructs the unordered map and the contained elementsunordered_map::operator=
- Assigns values to the unordered mapunordered_map::get_allocator
- Returns the allocator used to allocate memory for the elements
- Iterators
- Capacity
- Modifiers
unordered_map::clear
- Clears the contentsunordered_map::insert
- Inserts elementsunordered_map::emplace
- Constructs elements in-placeunordered_map::emplace_hint
- Constructs elements in-place using a hintunordered_map::erase
- Erases elementsunordered_map::swap
- Swaps the contents with another unordered map
- Lookup
unordered_map::count
- Returns the number of elements matching specific keyunordered_map::find
- Finds an element with specific keyunordered_map::equal_range
- Returns a range of elements matching specific key
- Bucket interface
- ...
- Hash policy
- ...
- Observers
unordered_map::hash_function
- Returns the function used to create hash of a keyunordered_map::key_eq
- Returns key comparison function
Description
template<class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;
unordered_map
takes at least two and at most five template parameters, called Key, T, Hash, Pred and Alloc.
unordered_map
will associate elements of type- Key
- the type of the unique identifier for an element in the map (for duplicate keys,
unordered_multimap
is used). - T
- the type of the element stored in the map
- [Hash]
- a hash function object. With
Hash h; Key k;
the callh(k)
should return an integer of typesize_t
. The probability thath(k1) == h(k2)
for two different keysk1, k2
should be minimal. See section 20.2.4 in the C++0x proposal. C++0x comes with a hash function objectstd::hash<Key>
which is implemented for standard types.[2] - [Pred]
- a function which compares two keys for equality: this is needed for custom data types to resolve hash collisions, which occur when two elements hash to the same value but are actually different.
- [Alloc]
- a function which allocates new elements.
Example
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<std::string, int> months;
months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;
std::cout << "september -> " << months["september"] << std::endl;
std::cout << "april -> " << months["april"] << std::endl;
std::cout << "december -> " << months["december"] << std::endl;
std::cout << "february -> " << months["february"] << std::endl;
return 0;
}
References
- ^ WG21 (9 April 2003), A Proposal to Add Hash Tables to the Standard Library (revision 4), n1456
{{citation}}
: CS1 maint: numeric names: authors list (link) - ^ a b WG21 (2010-08-21), Working Draft, Standard for Programming Language C++ (PDF), n3126
{{citation}}
: CS1 maint: numeric names: authors list (link) - ^ "hash_map<Key, Data, HashFcn, EqualKey, Alloc>". SGI. Retrieved 26 January 2011.
- ^ "libstdc++: hash_map Class Template Reference". Retrieved 26 January 2011.
- ^ "Class template unordered_map". Boost. Retrieved 26 January 2011.