Unordered maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys. The elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values. Internally unordered_map is implemented using Hash Table, the key provided to map are hashed into indices of hash table that is why performance of data structure depends on hash function but on an average the cost of search, insert and delete from hash table is O(1).

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

Important Methods

  • mapped_type& at ( const key_type& k );
    const mapped_type& at ( const key_type& k ) const;
    Returns a reference to the mapped value of the element with key k in the unordered_map.
  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator pointing to the first element in the unordered_map container.
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator pointing to the past-the-end element in the unordered_map container.
  • (1) iterator erase ( const_iterator position );
    (2) size_type erase ( const key_type& k );
    (3) iterator erase ( const_iterator first, const_iterator last );
    Removes from the unordered_map container either a single element or a range of elements ([first,last)).
  • iterator find ( const key_type& k );
    const_iterator find ( const key_type& k ) const;
    Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).
  • size_type size() const noexcept;
    Returns the number of elements in the unordered_map container.
  • void clear() noexcept;
    All the elements in the unordered_map container are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.

 

Example

// Demonstrate functionality of unordered_map 
#include <iostream> 
#include <unordered_map> 
using namespace std; 

int main() 
{ 
  // Key of string type and value of double type 
  unordered_map<string, int> umap; 
  unordered_map<string, int> mypantry = {{"milk",2},{"flour",1}};
              
  // Inserting values by using [] operator 
  umap["Ten"] = 10; 
  umap["Twenty"] = 20; 
  umap["Thirty"] = 30; 

  // Traversing an unordered map 
  for (auto x : umap)
    cout << x.first << " " << x.second << endl; 
    
  // Inserting value by insert function 
  umap.insert(make_pair("e", 2.718));   
  
  // If key not found in map iterator to end is returned 
  if (umap.find("E") == umap.end()) 
    cout << "E" << " not found"; 
    
  // Iterating over all value of umap 
  unordered_map<string, double>:: iterator itr; 
  for (itr = umap.begin(); itr != umap.end(); itr++) 
  { 
    cout << itr->first << " " << itr->second << endl; 
  }
  
  // Range insertion
  umap.insert (mypantry.begin(), mypantry.end());
  
  // Initializer list insertion
  umap.insert ({{"sugar",8},{"salt",1}} );
  
}