declaration
template<
class Key,
class T,
class Hash = std::hash<key>,
class KeyEqual = std::equal_to<key>,
class Allocator = std::allocator<std::pair<const>>
> class unordered_map;
Key is the type of the keysT is the type of the mapped values
Hash is the hash function object
KeyEqual is the equality comparison function
Allocator is the allocator used to manage memory
member types
| Member type | Definition | Explanation |
|---|---|---|
| key_type | Key | Type of the keys. |
| mapped_type | T | Type of the mapped values. |
| value_type | std::pair<const>Key, T> | Type of elements stored in the container. |
| hasher | Hash | The hash function type. |
| key_equal | KeyEqual | The equality comparison function type. |
| allocator_type | Allocator | Allocator type. |
| size_type | Unsigned integer type (usually std::size_t) | Used for sizes and indices. |
| difference_type | Signed integer type (usually std::ptrdiff_t) | Used for differences between iterators. |
| reference | value_type& | Reference to an element, allows modification. |
| const_reference | const value_type& | Const reference to an element, read-only access. |
| iterator | Iterator to value_type | Traverses and modifies elements. |
| const_iterator | Const iterator to value_type | Traverses elements without modification. |
member functions
element access
-
mapped_type& at(const Key& key)Returns a reference to the value corresponding to key. Throws
std::out_of_rangeif key not found. -
mapped_type& operator[](const Key& key)Returns a reference to the value corresponding to key. If key does not exist, a new element is inserted.
iterators
-
iterator begin()Returns an iterator to the first element.
-
iterator end()Returns an iterator to one past the last element.
-
const_iterator cbegin() constReturns a const iterator to the first element.
-
const_iterator cend() constReturns a const iterator to one past the last element.
capacity
-
bool empty() constReturns true if the container has no elements.
-
size_type size() constReturns the number of elements.
-
size_type max_size() constReturns the maximum number of elements the container can hold.
-
void reserve(size_type n)Reserves space for at least n elements.
-
size_type bucket_count() constReturns the number of buckets in the hash table.
-
float load_factor() constReturns the average number of elements per bucket.
-
float max_load_factor() constReturns the maximum allowed load factor.
-
void rehash(size_type count)Sets the number of buckets to at least count.
modifiers
-
void clear()Removes all elements from the container.
-
std::pair<iterator, bool> insert(const value_type& value)Inserts an element. Returns a pair consisting of an iterator to the element and a bool indicating whether insertion took place.
-
template<typename> void insert(InputIt first, InputIt last)Inserts elements from the range [first, last).
-
void erase(iterator pos)Removes the element at the given iterator position.
-
size_type erase(const Key& key)Removes the element with the specified key. Returns the number of elements removed (0 or 1).
-
void swap(unordered_map<Key, T>& other)Exchanges the contents with another unordered_map.