Unordered Map vs Map in C++

Smita Sen
Catalysts Reachout
Published in
5 min readSep 16, 2022

While writing the code, many of you must be confused about whether to use a map or an unordered map. There is a significant difference between maps and unordered maps in terms of efficiency. To choose which one should be used, we must understand how they both operate internally.

Let us first learn about map then we will move forward to learn about unordered_map and next we will see differences between map and unordered_map.

Maps are associative containers that store elements in a mapped fashion.Map is an ordered sequence of unique keys. Each element has a key value and a mapped value. No two mapped values can have the same key values. The map is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal). The time complexity of map operations is O(log n).

List of all Functions of map

insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the map container –> O(log n)

count() is a built-in function in C++ STL which returns 1 if the element with key K is present in the map container. It returns 0 if the element with key K is not present in the container.

equal_range() Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k.

erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range –> O(log n)

rend() Returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end).

rbegin() Returns a reverse iterator which points to the last element of the map.

find() Returns an iterator to the element with key-value ‘g’ in the map if found, else returns the iterator to end.

crbegin() and crend() crbegin() returns a constant reverse iterator referring to the last element in the map container. crend() returns a constant reverse iterator pointing to the theoretical element before the first element in the map.

cbegin() and cend() cbegin() returns a constant iterator referring to the first element in the map container. cend() returns a constant iterator pointing to the theoretical element that follows the last element in the multimap.

emplace()Inserts the key and its element in the map container.

max_size() Returns the maximum number of elements a map container can hold –> O(1)

upper_bound() Returns an iterator to the first element that is equivalent to mapped value with key-value ‘g’ or definitely will go after the element with key-value ‘g’ in the map.

operator= Assigns contents of a container to a different container, replacing its current content.

lower_bound() Returns an iterator to the first element that is equivalent to the mapped value with key-value ‘g’ or definitely will not go before the element with key-value ‘g’ in the map –> O(log n)

emplace_hint() Inserts the key and its element in the map container with a given hint.

value_comp() Returns the object that determines how the elements in the map are ordered (‘<‘ by default).

key_comp() Returns the object that determines how the elements in the map are ordered (‘<‘ by default).

size() Returns the number of elements in the map.map::empty()Returns whether the map is empty

begin() and end() begin() returns an iterator to the first element in the map. end() returns an iterator to the theoretical element that follows the last element in the map

operator[] This operator is used to reference the element present at the position given inside the operator.

clear() Removes all the elements from the map.

at() and swap() at() function is used to return the reference to the element associated with the key k. swap() function is used to exchange the contents of two maps but the maps must be of the same type, although sizes may differ.

unordered_map is an associated container that stores elements formed by the combination of key-value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined. Internally unordered_map is implemented using Hash Table, the key provided to map are hashed into indices of a hash table that is why the performance of data structure depends on hash function a lot but on an average, the cost of search, insert and delete from the hash table is O(1).

List of all Functions of unordered_map

at() This function in C++ unordered_map returns the reference to the value with the element as key k.

begin() Returns an iterator pointing to the first element in the container in the unordered_map container

end() Returns an iterator pointing to the position past the last element in the container in the unordered_map container

bucket() Returns the bucket number where the element with the key k is located in the map.

bucket_count bucket_count is used to count the total no. of buckets in the unordered_map. No parameter is required to pass into this function.

bucket_size Returns the number of elements in each bucket of the unordered_map.

count() Count the number of elements present in an unordered_map with a given key.

equal_range Return the bounds of a range that includes all the elements in the container with a key that compares equal to k.

find() Returns iterator to element.

empty() checks whether container is empty in the unordered_map container.

erase() erase elements in the container in the unordered_map container.

Now let us see the differences

Use std::map when

  • You need ordered data.
  • You would have to print/access the data (in sorted order).
  • You need predecessor/successor of elements.

Use std::unordered_map when

  • You need to keep count of some data (Example — strings) and no ordering is required.
  • You need single element access i.e. no traversal.

Happy learning!!

--

--