Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
This blog will learn about the Unordered Multimap in STL concept in the C++ programming language.
We recommend that you visit here to learn more about the basic functionality of the Map Container in STL.
Unordered Multimap
Unordered multimap is an associative container. It holds key and value pairs in the same way as an unordered map does. However, repeated values are not permitted in an unordered map but they are allowed in multimap. There is no order in the process of storing items because these are unordered containers. However, duplicates are allowed, thus items with the same key are grouped together in the same bucket. Each key-value pair has a different count value for duplicate keys.
As unordered multimap stores key-value pairs in a hash table, the temporal complexity is determined by the internal hash algorithm. In the average situation, it takes constant time, and in the worst case, it takes a linear time for any operation.
Syntax
The following is the syntax of unordered_multimap in C++ STL
Some of the frequently used methods of unordered_multimap are listed below:
begin() – Returns an iterator that points to the container's initial element or one of its buckets' first element.
end() – Returns an iterator pointing to the position after the container's last element or after one of its bucket's last elements.
count() – Returns the number of entries in the container whose key matches the parameter's key.
cbegin()– Returns a constant iterator pointing to the container's initial element or one of its buckets' first element.
cend()– Returns a constant iterator pointing to the position after the container's last element or after one of its bucket's last elements.
clear() – The unordered multimap container's contents are cleared.
size() – Returns the unordered multimap's size. It indicates how many items are in the container.
swap() — Swaps two unordered multimap containers' contents. Both containers' sizes might be different.
bucket size() – Returns the number of elements in the bucket n. find()– Returns an iterator that points to one of the entries with the key k.
find() – If the unordered multimap container is empty, this function returns true. Otherwise, false is returned.
max size() – This function returns the maximum number of items that the unordered multimap container can store.
emplace() – In the unordered multimap container, it adds a new key element.
We are using Some of these methods in the below example.
Example:
#include <bits/stdc++.h>
using namespace std;
// making typedef for short declaration
typedef unordered_multimap<string, int>::iterator unit;
//function to print map
void printing_map(unordered_multimap<string, int> ummp)
{
//iterating over the map
for (auto it = ummp.begin(); it != ummp.end(); ++it)
cout << it->first << " = " << it->second << endl;
cout << endl;
}
int main()
{
// Initialization
unordered_multimap<string, int> mp1(
{
{ "apple", 13 },
{ "ball", 12 },
{ "apple", 11 },
{ "cat", 17 },
{ "dog", 19 },
{ "cat", 16 },
{ "apple", 11 }
}
);
printing_map(mp1);
//total number of elements in map
cout << "Size of map is "<< mp1.size() << endl;
string key = "cat";
// finding pair associated with key
unit it = mp1.find(key);
if (it != mp1.end()) {
cout<<"key "<<key<<" is there in map\n";
cout<<"value associated with "<<key<<" is "<<it->second<<endl;
}
else
cout<<"key "<<key<<" is not present\n"<<endl;
// count of number of pair with key
int cnt = mp1.count(key);
cout<<"Number of values with "<< key<<" are "<<cnt<<"\n";
// one insertion by making pair explicitly
mp1.insert(make_pair("dog", 13));
// insertion by initializer list
mp1.insert({ { "alpha", 11 }, { "beta", 23 } });
cout << "\nAfter insertion\n";
printing_map(mp1);
// erase deletes all pairs corresponding to key
mp1.erase("apple");
cout << "After deletion of apple\n";
printing_map(mp1);
// clear deletes all pairs from container
mp1.clear();
if (mp1.empty())
cout<<"map is empty";
else
cout<<"map is not empty";
}
You can also try this code with Online C++ Compiler
dog = 19 cat = 16 cat = 17 ball = 12 apple = 11 apple = 11 apple = 13
Size of map is 7 key cat is there in map value associated with cat is 16 Number of values with cat are 2
After insertion beta = 23 alpha = 11 apple = 11 apple = 11 apple = 13 ball = 12 cat = 16 cat = 17 dog = 13 dog = 19
After deletion of apple beta = 23 alpha = 11 ball = 12 cat = 16 cat = 17 dog = 13 dog = 19
map is empty
Time Complexity
On average, all operations on unordered multimap take the same amount of time depending on the internally used hash function. Time can grow to linear in the worst scenario. However, unordered multimap beats multimap (tree-based multimap).
In the typical situation, linear, i.e., O(n).
In the worst scenario, quadratic, i.e., O(n^2).
This concludes our topic of Unordered Multimap in C++. Now let’s move on to the FAQ section.
FAQS
What is an unordered multimap in C++? Unordered multimaps, like unordered map containers, are associative containers that hold items produced by combining a key value and a mapped value. Still, they enable distinct elements to have equivalent keys.
How is unordered_multimap implemented in C++? The core implementation of an unordered multimap is the same as that of an unordered map, except each key-value pair has a separate count value for duplicate keys.
What exactly is the distinction between a map and a multimap? The map and the multimap are containers for managing key/value pairs as a single component. The main difference is that keys in a map must be unique, but keys in a multimap can be duplicated.
Is multimap sorted in C++? Yes, multimap is an associative container that holds a sorted list of key-value pairs.
Why is it called Unordered Map? It is called an Unordered Map because the items in this map are kept in random order. The keys are not stored in any particular order, hence the name.
Key Takeaways
In this article, we have extensively discussed unordered multimap in Cpp and have seen its various methods and examples. Visit here to learn more.
We hope that this blog has helped you enhance your knowledge regarding Unordered Multimap and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!
Live masterclass
Amazon PowerBI & AI Essentials: Data Visualization Tips
by Abhishek Soni
01 May, 2025
01:30 PM
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
28 Apr, 2025
01:30 PM
JioHotstar Sports Analytics: IPL Dataset
by Prerita Agarwal
29 Apr, 2025
01:30 PM
Google SDE interview: Tips to get shortlisted
by Shantanu Shubham
30 Apr, 2025
01:30 PM
Amazon PowerBI & AI Essentials: Data Visualization Tips