From Surf Wiki (app.surf) — the open knowledge base
Unordered associative containers (C++)
Group of class templates in the C++ Standard Library
Group of class templates in the C++ Standard Library
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_setstd::unordered_mapstd::unordered_multisetstd::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 (for polymorphic memory resources). 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 (respectively) 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 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 the Container concept, which means they have begin(), end(), size(), max_size(), empty(), and swap() methods.
| `unordered_set` | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| (C++11) | `unordered_map` | |||||||||||
| (C++11) | `unordered_multiset` | |||||||||||
| (C++11) | `unordered_multimap` | |||||||||||
| (C++11) | Description | Element access | Iterators | Capacity | Modifiers | Lookup | Bucket interface | Hash policy | Observers | |||
| (constructor) | (constructor) | (constructor) | (constructor) | Constructs the container from variety of sources | ||||||||
| (destructor) | (destructor) | (destructor) | (destructor) | Destructs the set and the contained elements | ||||||||
| `operator=` | `operator=` | `operator=` | `operator=` | Assigns values to the container | ||||||||
| `get_allocator` | `get_allocator` | `get_allocator` | `get_allocator` | Returns the allocator used to allocate memory for the elements | ||||||||
| `at` | Accesses specified element with bounds checking. | |||||||||||
| `[operator[](https://en.cppreference.com/w/cpp/container/unordered_map/operator_at)]` | Accesses specified element without bounds checking. | |||||||||||
| `begin` | `begin` | `begin` | `begin` | Returns an iterator to the beginning of the container | ||||||||
| `end` | `end` | `end` | `end` | Returns an iterator to the end of the container | ||||||||
| `empty` | `empty` | `empty` | `empty` | Checks whether the container is empty | ||||||||
| `size` | `size` | `size` | `size` | Returns number of elements in the container. | ||||||||
| `max_size` | `max_size` | `max_size` | `max_size` | Returns the maximum possible number of elements in the container | ||||||||
| `clear` | `clear` | `clear` | `clear` | Clears the contents. | ||||||||
| `insert` | `insert` | `insert` | `insert` | Inserts elements. | ||||||||
| `emplace` | `emplace` | `emplace` | `emplace` | Constructs elements in-place (C++11) | ||||||||
| `emplace_hint` | `emplace_hint` | `emplace_hint` | `emplace_hint` | Constructs elements in-place using a hint (C++11) | ||||||||
| `erase` | `erase` | `erase` | `erase` | Erases elements. | ||||||||
| `swap` | `swap` | `swap` | `swap` | Swaps the contents with another container. | ||||||||
| `count` | `count` | `count` | `count` | Returns the number of elements matching specific key. | ||||||||
| `find` | `find` | `find` | `find` | Finds an element with specific key. | ||||||||
| `equal_range` | `equal_range` | `equal_range` | `equal_range` | Returns a range of elements matching specific key. | ||||||||
| ... | ||||||||||||
| ... | ||||||||||||
| `hash_function` | `hash_function` | `hash_function` | `hash_function` | Returns the function used to create hash of a key | ||||||||
| `key_eq` | `key_eq` | `key_eq` | `key_eq` | Returns key comparison function. |
Usage example
import std;
using std::string;
using std::unordered_map;
const unordered_map<string, int> MONTHS {
{"January", 31},
{"February", 28},
{"March", 31},
{"April", 30},
{"May", 31},
{"June", 30},
{"July", 31},
{"August", 31},
{"September", 30},
{"October", 31},
{"November", 30},
{"December", 31}
};
int main(int argc, char* argv[]) {
std::println("September -> {}", MONTHS["September"]);
std::println("April -> {}", MONTHS["April"]);
std::println("December -> {}", MONTHS["December"]);
std::println("February -> {}", MONTHS["February"]);
return 0;
}
Custom hash functions
To use custom objects in std::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 {
int i;
int j;
int k;
};
struct HashVector3 {
size_t operator()(const Vector3& x) const {
return hash<int>()(x.i) ^ hash<int>()(x.j) ^ hash<int>()(x.k);
}
};
The user defined function can be used as is in std::unordered_map, by passing it as a template parameter
unordered_map<Vector3, int, HashVector3> myPointToIntMap;
Or can be set as the default hash function by specializing std::hash:
namespace std {
template <>
class hash<Vector3> {
public:
size_t operator()(const Vector3& x) const {
return hash<int>()(x.i) ^ hash<int>()(x.j) ^ hash<int>()(x.k);
}
};
}
//...
unordered_map<Vector3, int> myPointToIntMap;
References
References
- "hash_map<Key, Data, HashFcn, EqualKey, Alloc>". [[Silicon Graphics]] (SGI).
- "libstdc++: hash_map Class Template Reference".
- Austern, Matthew. (9 April 2003). "A Proposal to Add Hash Tables to the Standard Library (revision 4)".
- Becker, Pete. (21 August 2010). "Working Draft, Standard for Programming Language C++".
- "Class template unordered_map". Boost.
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Unordered associative containers (C++) — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report