Multimaps

Multimaps

 
In C++, a multimap is a container class template from the Standard Template Library (STL) that stores elements in a sorted order.
A multimap allows multiple values to be associated with a single key, unlike a map which stores only one value per key. Each element in a multimap is a pair of a key and a value. The keys are sorted according to their comparison operator, which is by default the less-than operator.
Multimaps are implemented as binary search trees, which provides efficient insertion, deletion, and searching of elements. They have a time complexity of O(log n) for these operations.
Multimaps are useful in situations where you need to store multiple values associated with a single key. For example, you could use a multimap to store the names of all employees in a company along with their department. You could use the department name as the key and store the names of all employees in that department as the values.
 
 
#include <iostream> #include <map> using namespace std; int main() { // Create a multimap of string keys and integer values multimap<string, int> myMultimap; // Insert elements in the multimap myMultimap.insert(make_pair("A", 10)); myMultimap.insert(make_pair("B", 20)); myMultimap.insert(make_pair("B", 30)); myMultimap.insert(make_pair("C", 40)); myMultimap.insert(make_pair("C", 50)); // Print the elements in the multimap cout << "Current multimap elements: " << endl; for (auto it = myMultimap.begin(); it != myMultimap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Access elements in the multimap with a specific key cout << "Elements in multimap with key B: " << endl; auto range = myMultimap.equal_range("B"); for (auto it = range.first; it != range.second; it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Erase all elements with a specific key myMultimap.erase("C"); // Print the modified multimap cout << "Modified multimap after erasing all elements with key C: " << endl; for (auto it = myMultimap.begin(); it != myMultimap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Clear all of the elements of the multimap myMultimap.clear(); // Check if the multimap is empty if (myMultimap.empty()) { cout << "Multimap is empty" << endl; } else { cout << "Multimap is not empty" << endl; } return 0; }
This implementation first creates an empty multimap of string keys and integer values myMultimap. It then adds elements to the multimap using the insert() function. The elements are printed to the console using a for loop and the begin() and end() functions of the multimap. Note that the multimap can contain multiple values associated with the same key.
The equal_range() function is used to access all elements in the multimap with a specific key. The erase() function removes all elements from the multimap with a specific key. The modified multimap is printed to the console.
The clear() function deletes all of the elements of the multimap, and the empty() function checks if the multimap is empty.
 
 
PRACTICE PROBLEMS :
  1. Happy Numbers