C++ multimaps
In C++, a multimap is an associative container that allows multiple elements with the same key. Unlike the std::map
, where each key is unique, the std::multimap
allows duplicate keys. The elements are automatically sorted according to the key, and they are stored in ascending order by default.
A multimap stores key-value pairs, where the key can appear more than once. It uses a self-balancing binary search tree (often a Red-Black Tree) for efficient operations such as insertion, deletion, and search. Like a map
, operations in a multimap
are performed in logarithmic time (O(log n)).
The syntax to declare a multimap is:
std::multimap<KeyType, ValueType> multimap_name;
Where:
KeyType
: The type of the key (e.g., int
, std::string
).ValueType
: The type of the value associated with each key.You can also specify a custom comparator to order the keys:
std::multimap<KeyType, ValueType, Compare> multimap_name;
Where:
Compare
is a custom comparison function for the keys.
#include <iostream>
#include <map>
int main() {
// Declare a multimap that associates an integer key with a string value
std::multimap<int, std::string> multimap;
// Inserting elements into the multimap
multimap.insert({1, "Apple"});
multimap.insert({1, "Banana"});
multimap.insert({2, "Cherry"});
multimap.insert({2, "Date"});
multimap.insert({3, "Elderberry"});
// Displaying the elements in the multimap
for (const auto& pair : multimap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Output:
1: Apple
1: Banana
2: Cherry
2: Date
3: Elderberry
Explanation:
1
appears twice with two different values: "Apple"
and "Banana"
. Similarly, the key 2
appears twice.You can insert elements into a multimap using the insert()
method. A multimap allows duplicate keys:
multimap.insert({1, "Grapes"}); // Key 1 is duplicated with value "Grapes"
multimap.insert({2, "Fig"}); // Key 2 is duplicated with value "Fig"
You can remove elements by key or by iterator.
multimap.erase(1); // Removes all elements with key 1
auto it = multimap.find(2);
multimap.erase(it); // Removes the element pointed to by the iterator
You can search for an element by key using the find()
function. This function returns an iterator to the first element with the given key, or multimap.end()
if the key does not exist.
auto it = multimap.find(2);
if (it != multimap.end()) {
std::cout << "Found: " << it->first << " => " << it->second << std::endl;
} else {
std::cout << "Key not found!" << std::endl;
}
You can count the number of elements with a particular key using the count()
function. Since a multimap can have duplicate keys, count()
will return the number of elements with the given key.
std::cout << "Count of key 1: " << multimap.count(1) << std::endl; // Output: 2
std::cout << "Count of key 3: " << multimap.count(3) << std::endl; // Output: 1
You can check the size of the multimap and whether it is empty:
std::cout << "Size of multimap: " << multimap.size() << std::endl;
std::cout << "Is multimap empty? " << (multimap.empty() ? "Yes" : "No") << std::endl;
You can remove all elements from the multimap using the clear()
method:
multimap.clear(); // Removes all elements from the multimap
You can iterate through the elements of a multimap using an iterator or a range-based for loop:
for (auto it = multimap.begin(); it != multimap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
Example 2: Using Range-Based For Loop
for (const auto& pair : multimap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
By default, the std::multimap
stores elements in ascending order based on the keys. However, you can define a custom sorting order by providing a comparator.
#include <iostream>
#include <map>
bool descendingOrder(int a, int b) {
return a > b; // Custom comparator for descending order
}
int main() {
// Create a multimap with a custom comparator
std::multimap<int, std::string, bool(*)(int, int)> multimap(descendingOrder);
multimap.insert({1, "Apple"});
multimap.insert({2, "Banana"});
multimap.insert({3, "Cherry"});
// Display elements in descending order
for (const auto& pair : multimap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Output:
3: Cherry
2: Banana
1: Apple
std::multimap
std::map
, a std::multimap
allows multiple elements to have the same key, making it suitable for applications where duplicates are needed.map
, operations such as search, insertion, and deletion are performed in logarithmic time (O(log n)) due to the underlying self-balancing binary search tree.std::multimap
Use a std::multimap
when: