Multimap in C++ STL
Multimap is an associative container similar to map, but it can have multiple elements with same keys. It stores all the elements in increasing order based on their keys by default but can be changed if required.
Syntax
Multimap is defined as the std::multimap class template inside the <map> header file.
multimap<key_type, value_type, comp> mm;
where,
- key_type: Data type of key.
- value_type: Data type of value.
- comp: Custom comparator function that defines how to compare two keys for sorting. It is optional and if not provided, data is sorted in increasing order of the keys.
- mm: Name assigned to the multimap.
Declaration and Initialization
We can declare and initialize a multimap in different ways as shown in the below example:
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating an empty multimap
multimap<int, string> mm1;
// Creating multimap using initializer list
multimap<int, string> mm2 = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
for (auto i : mm2)
cout << i.first << ": " << i.second
<< endl;
return 0;
}
Output
1: Geeks 1: C++ 2: For
Basic Operations
Basic operations on multimap containers are shown below:
Inserting Elements
A key-value pair can be inserted into multimap using insert() method. Insertion using [] operator is not valid because there can be multiple elements with same key.
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm;
// Inserting key-value pairs
mm.insert({1, "Geeks"});
mm.insert({2, "For"});
mm.insert({1, "C++"});
for(auto x: mm)
cout << x.first << ": " << x.second
<< endl;
return 0;
}
Output
1: Geeks 1: C++ 2: For
Accessing Elements
Elements are accessed only through iterators, using ->first for keys and ->second for values, since [] is not available.
You can move iterators with next() or advance(), and quickly get the first and last elements using begin() and end().
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
// Accessing 1st element
auto f = mm.begin();
cout << f->first << ": " << f->second
<< endl;
// Accessing 2nd element
auto it = next(f, 1);
cout << it->first << ": " << it->second;
return 0;
}
Output
1: Geeks 1: C++
Updating Elements
In multimap, the key of any element cannot be modified. But we can modify the value using the iterator to that element.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
// Updating 2nd element value
auto it = next(mm.begin(), 1);
it->second = "Java";
for (auto x: mm)
cout << x.first << ": " << x.second
<< endl;
return 0;
}
Output
1: Geeks 1: Java 2: For
Traversing
Multimap can be traversed by either range-based for loop or using begin() and end() iterators with a loop.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
// Traverse multimap
for(auto it = mm.begin(); it != mm.end(); it++)
cout << it->first << " " << it->second
<< "\n";
return 0;
}
Output
1 Geeks 1 C++ 2 For
Finding Elements
We can quickly find the first element with a given key using the find() method.
If the key isn't found, it returns the end() iterator.
To find a specific element among duplicates, use the range from equal_range() to search within that group.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
// Find element with key 2
auto it = mm.find(2);
cout << it->first << ": " << it->second;
return 0;
}
Output
2: For
Deleting Elements
Elements can be removed from multimap using erase() function either by passing the key or iterator. If the key is passed, then all the elements with that key are removed. If the iterator is passed, then only the pointed element is removed.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
multimap<int, string> mm = {{1, "Geeks"},
{2, "For"}, {1, "C++"}};
// Delete element using key
mm.erase(2);
// Delete element using iterator
mm.erase(mm.find(1));
for (auto x: mm)
cout << x.first << ": " << x.second;
return 0;
}
Output
1: C++
Time Complexity
The below table lists the time complexity of the above operations on multimap:
| Operation | Time Complexity |
|---|---|
| Insert an element | O(log n) |
| Delete an element | O(log n) |
| Access an element at any position. | O(n) |
| Find an element by key | O(log n) |
Find the number of elements with a specific key | O(log n) |
| Traverse the multimap | O(n) |
Multimap vs Map
Primary difference between multimap and map is shown below:
Feature | map | multimap |
|---|---|---|
Key Uniqueness | Keys are unique | Duplicate keys are allowed |
Access | Supports [] and at() | Does not support [] or at() |
Order | Elements sorted by key | Elements sorted by key |
Storage | One value per key | Multiple values per key |
Use Case | When each key maps to one value | When keys can map to multiple values |