In C++, an unordered_multimap is another container class template from the Standard Template Library (STL) that stores elements in an unordered manner.
Unlike a multimap, which stores elements in a sorted order, an unordered_multimap does not guarantee any specific order of the elements. Instead, it uses a hash function to determine the position of elements in the container.
An unordered_multimap also allows multiple values to be associated with a single key, similar to a multimap. However, unlike a multimap, the order of the elements with the same key is not guaranteed.
Unordered_multimaps are implemented using hash tables, which provide efficient insertion, deletion, and searching of elements. They have an average time complexity of O(1) for these operations. However, their worst-case time complexity can be as bad as O(n) in certain situations.
Unordered_multimaps are useful in situations where you do not need the elements to be stored in a specific order, but still require fast insertion, deletion, and searching of elements. For example, you could use an unordered_multimap to store the names of all employees in a company along with their department, if you do not need the department names to be sorted.
#include <iostream> #include <unordered_map> #include <vector> using namespace std; int main() { // Create an unordered multimap of string keys and integer values unordered_multimap<string, int> myUnorderedMultimap; // Insert elements in the unordered multimap myUnorderedMultimap.insert(make_pair("A", 10)); myUnorderedMultimap.insert(make_pair("B", 20)); myUnorderedMultimap.insert(make_pair("B", 30)); myUnorderedMultimap.insert(make_pair("C", 40)); myUnorderedMultimap.insert(make_pair("C", 50)); // Print the elements in the unordered multimap cout << "Current unordered multimap elements: " << endl; for (auto it = myUnorderedMultimap.begin(); it != myUnorderedMultimap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Access elements in the unordered multimap with a specific key cout << "Elements in unordered multimap with key B: " << endl; auto range = myUnorderedMultimap.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 myUnorderedMultimap.erase("C"); // Print the modified unordered multimap cout << "Modified unordered multimap after erasing all elements with key C: " << endl; for (auto it = myUnorderedMultimap.begin(); it != myUnorderedMultimap.end(); it++) { cout << it->first << ": " << it->second << endl; } cout << endl; // Clear all of the elements of the unordered multimap myUnorderedMultimap.clear(); // Check if the unordered multimap is empty if (myUnorderedMultimap.empty()) { cout << "Unordered multimap is empty" << endl; } else { cout << "Unordered multimap is not empty" << endl; } return 0; }
This implementation creates an empty unordered multimap of string keys and integer values
myUnorderedMultimap
. It then adds elements to the unordered multimap using the insert()
function. The elements are printed to the console using a for loop and the begin()
and end()
functions of the unordered multimap. Note that the unordered multimap can contain multiple values associated with the same key.The
equal_range()
function is used to access all elements in the unordered multimap with a specific key. The erase()
function removes all elements from the unordered multimap with a specific key. The modified unordered multimap is printed to the console.The
clear()
function deletes all of the elements of the unordered multimap, and the empty()
function checks if the unordered multimap is empty. Finally, the program returns 0.PRACTICE PROBLEMS :