C++ STL Unordered Multimap – std::unordered_multimap

In this tutorial we will learn about stl unordered multimap container in C++.

Unordered multimap is an associative container. Same as unordered map it stores key and value pair. But unordered map doesn’t allow duplicate values. Here duplicate values are allowed. Since these are unordered containers, there is no order in the way of storing elements. But this allows duplicates so the elements which have same key are grouped together in the same bucket. For duplicate keys another count value is maintained with each key – value pair.

Since unordered multimap uses hash table to store key – value pairs, time complexity based on internal hash function used. It performs constant time in average case and in worst case it will take linear time for any operation.

C++ STL Unordered Multimap – std::unordered_multimap

Iterators that can be applicable on unordered multimap:

begin(): returns iterator to the beginning.

end(): returns iterator to the end of the container.

cbegin(): Returns constant iterator to the beginning.

cend(): Returns constant iterator to the end.

Unordered multimap is just an extra feature of unordered map. So we need to include same unordered map header function to work with unordered multimap.

i.e #include <unordered_map>

Now see some functions applicable on unordered multimap:

insert()

There are different types of insertions. In any type we should represent elements in a pair like structure. Since this stores key value pairs.

i.e { “Key” , “value” } format.

Different inserting ways:

  • We can directly initialize key – value pairs placing “=” symbol after declaration.
  • Using “insert” function also we can insert.
  • We can make pair using std :: pair and we can insert this pair into unordered multimap.
  • Or we can directly move the pair.
  • If there is another containers which have key – value pairs, by specifying a range we can copy those pairs into to unordered multimap.

erase()

Same as insertion, we can perform deletion also in different ways.

  • Deleting by pointing one of the pair using iterator.
  • Deleting using “key”.

clear(): Clear operation used to remove all key value pairs. By result container becomes empty.

empty(): empty() is a Boolean function. It is used to check whether the container empty or not?

Example program to show above functions:

Output

Unordered multimap contains:
(Gayle , 215)
(Sehwag , 219)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
(Guptil , 237)
Erasing first pair
Erasing record contains key as “Guptil”
After performing above erase operations….
(Sehwag , 219)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
Applying clear() operation…
Checking unordered multi map empty or not
Unordered multi map is empty

size(): size() operation used to know how many key value pairs present in the unordered multimap.

max_size(): max_size() returns maximum size of the container.

find():

  • Find is used to search a particular data pair.
  • We give key as a parameter to this function.
  • If key is present iterator points to it’s position.
  • If given key is not present iterator points to end of the container.

count(): Count used will return how many times a pair present in the container with given key. We give key as a parameter to count function.

Example program to show above functions:

Output

Unordered multimap contains:
(Guptil , 237)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
Size of the unordered multimap is 4
Maximum Size of the unordered multimap is 329406144173384850
Finding “Guptil data ”
Key found and it’s value is 237

Counting how many times Rohit key is present
Rohit scored 200+ runs 3 times

swap(): swap() operation applied on two maps. Bu result all key – value pairs of unordered multi map1 are transferred to unordered multi map2 and vice –versa.

Example program to show swap operation:

Output

Unordered multimap :: 1 Before swap:
(Sehwag , 219)
(Guptil , 237)
(Rohit , 209)
(Rohit , 264)
Unordered multimap :: 2 Before swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)

Performing swap operation
Unordered multimap :: 1 After swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)
Unordered multimap :: 2 After swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)

Leave a Reply

Your email address will not be published. Required fields are marked *