Unordered Multisets

Unordered Multisets

 
In C++, an unordered_multiset is a container class template from the Standard Template Library (STL) that stores elements in an unordered manner.
An unordered_multiset stores multiple elements of the same value and allows duplicates. The position of each element in the container is determined by a hash function. This means that the elements are not sorted in any specific order.
Unordered_multisets 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_multisets 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_multiset to store the ages of all employees in a company. The unordered_multiset would quickly check whether a given age already exists in the container.
You can also use an unordered_multiset to implement various algorithms such as finding the frequency of each element in a set of numbers. The unordered_multiset container provides a set of member functions to perform these operations. However, because the elements are not sorted, the results of these operations may not be in any specific order.
 
 
#include <iostream> #include <unordered_set> #include <vector> using namespace std; int main() { // Create an unordered multiset of integer values unordered_multiset<int> myUnorderedMultiset; // Insert elements in the unordered multiset myUnorderedMultiset.insert(10); myUnorderedMultiset.insert(20); myUnorderedMultiset.insert(30); myUnorderedMultiset.insert(30); myUnorderedMultiset.insert(40); myUnorderedMultiset.insert(40); myUnorderedMultiset.insert(50); // Print the elements in the unordered multiset cout << "Current unordered multiset elements: " << endl; for (auto it = myUnorderedMultiset.begin(); it != myUnorderedMultiset.end(); it++) { cout << *it << endl; } cout << endl; // Count the number of occurrences of a value in the unordered multiset int count = myUnorderedMultiset.count(30); cout << "Number of occurrences of 30 in the unordered multiset: " << count << endl; cout << endl; // Erase all occurrences of a value from the unordered multiset myUnorderedMultiset.erase(40); // Print the modified unordered multiset cout << "Modified unordered multiset after erasing all occurrences of 40: " << endl; for (auto it = myUnorderedMultiset.begin(); it != myUnorderedMultiset.end(); it++) { cout << *it << endl; } cout << endl; // Clear all of the elements of the unordered multiset myUnorderedMultiset.clear(); // Check if the unordered multiset is empty if (myUnorderedMultiset.empty()) { cout << "Unordered multiset is empty" << endl; } else { cout << "Unordered multiset is not empty" << endl; } return 0; }
 
This implementation creates an empty unordered multiset of integer values myUnorderedMultiset. It then adds elements to the unordered multiset using the insert() function. The elements are printed to the console using a for loop and the begin() and end() functions of the unordered multiset. Note that the unordered multiset can contain multiple occurrences of the same value.
The count() function is used to count the number of occurrences of a value in the unordered multiset.
The erase() function removes all occurrences of a value from the unordered multiset. The modified unordered multiset is printed to the console.
The clear() function deletes all of the elements of the unordered multiset, and the empty() function checks if the unordered multiset is empty. Finally, the program returns 0.