C++ unordered_map Library
C++ unordered_map Functions
The <unordered_map> library has functions that let you add, find and remove elements in an unordered map. An unordered map stores elements in key/value pairs, and every key is unique. Unlike a map, the elements are not sorted, and they come in no particular order.
Many of the functions return an iterator (a pointer-like object that points to an element). For an unordered map element, it->first is the key and it->second is the value.
An unordered map keeps its elements in buckets (groups of elements). It calculates a number from each key (a hash) to choose the bucket. This is why it is fast to find a key. Use bucket_count(), load_factor() and reserve() to see and control the buckets.
| Function | Description |
|---|---|
| at() | Returns the value of the element with the given key |
| begin() | Returns an iterator to the first element of an unordered map |
| bucket_count() | Returns the number of buckets (groups that hold the elements) in an unordered map |
| clear() | Removes all elements from an unordered map |
| count() | Returns 1 if a key exists in an unordered map, and 0 if it does not |
| emplace() | Adds a new element to an unordered map, built from a key and a value |
| empty() | Checks whether an unordered map is empty or not |
| end() | Returns an iterator to the position after the last element of an unordered map |
| erase() | Removes one or more elements from an unordered map |
| find() | Searches for a key, and returns an iterator to the element |
| insert() | Adds one or more elements to an unordered map |
| load_factor() | Returns the average number of elements in each bucket of an unordered map |
| operator[] | Gets or changes the value of a key, and adds a new element if the key does not exist |
| reserve() | Makes room for at least a given number of elements in an unordered map |
| size() | Returns the number of elements in an unordered map |
| swap() | Swaps the contents of two unordered maps |