C++ unordered_multiset



std::unordered_multiset

An unordered_multiset is a container by the Standard Template Library (STL) in C++, which stores elements without any particular order and allows multiple occurrences or duplicate values of the same element. The <unordered_set> header file is used for both unordered_set and unordered_multiset containers.

Syntax

Here is the following syntax for declaring an unordered_multiset:

#include <unordered_set>
std::unordered_multiset<type> container_name;

Here,

    <type> is the data type of the elements you want to store in the unordered_multiset (e.g., int, string, etc.).
    container_name is the name of the unordered_multiset variable.

Example to Create unordered_multiset

The following example demonstrates how you can create an unordered multiset:

#include <unordered_set>
#include <iostream>
#include <string>

using namespace std;

int main() {
    // Declare unordered_multisets for integers and strings
    unordered_multiset<int> ums_int = {1, 2, 3, 3};
    unordered_multiset<string> ums_str = {"apple", "banana", "apple"};

    // Display both sets together (merged display)
    cout << "Merged unordered_multiset (Integers and Strings): ";
    for (const auto& elem : ums_int) {
        cout << elem << " ";
    }
    for (const auto& elem : ums_str) {
        cout << elem << " ";
    }
    cout << endl;

    return 0;
}

Output

Merged unordered_multiset (Integers and Strings): 3 3 2 1 banana apple apple

Member Functions of unordered_multiset

1. The insert() function

The insert() function is used to add one or more elements to the unordered_multiset, allowing duplicity.

Syntax

unordered_multiset<type> ums;
ums.insert(element);
ums.insert({element1, element2, ...});

2. The find() function

The find() function is used to check if an element exists in the unordered_multiset. It returns an iterator to the element if found; otherwise, it returns end().

Syntax

auto it = ums.find(element);
// Returns iterator to element or ums.end()

3. The count() function

The count() function returns the number of occurrences of an element in the unordered_multiset. Since this function allows duplicacy, it will return the total count of that element in the container.

Syntax

size_t count = ums.count(element);
// Returns number of occurrences of element

4. The erase() function

The erase() function removes one or more elements from the unordered_multiset. You can remove specific elements by value or use an iterator to remove a single element. For duplicates, only one occurrence will be removed for each call.

Syntax

ums.erase(element);  // Removes one occurrence of element
ums.erase(it);       // Removes the element at iterator `it`
ums.erase(ums.begin(), ums.end());  // Removes all elements in the range

5.The find() or count() function

This function will check if an element exists in the container.

Syntax

list.count(element);  // for lists
string.count(substring);  // for strings

6. The size() function

The size() function returns the number of elements currently in the unordered_multiset.

Syntax

size_t size = ums.size();
// Returns the number of elements in the set

7. The begin() and end() functions

To iterate, you can use iterators or range-based loops over the elements in an unordered_multiset. begin() and end() are used for accessing the first and last elements.

Syntax

for (auto it = ums.begin(); it != ums.end(); ++it) {
    // Access each element via *it
}

unordered_set Vs. unordered_multiset

  • unordered_set: A container that stores unique elements only, no duplicates are allowed.
  • unordered_multiset: A container that allows multiple occurrences (duplicates) of the same element.

Average Time Complexity of unordered_multiset

unordered_multiset is implemented using a hash table data structure. This results in fast access because elements are hashed into "buckets" based on their hash values. This unordered_multiset provides constant time complexity, i.e., O(1), for some generic operations like lookups, insertions, and deletions.

Hash collisions lead to the worst time complexity O(n). This is because, in a collision, multiple elements are hashed into the same bucket, which results in a linear search through all the elements in that bucket. This overall results in a significant degradation of performance.

Use Cases of unordered_multiset

  • It is used when you want to store multiple occurrences of the same element.
  • It is suitable for counting the frequency of items or when duplicates are required.
  • It is efficient for quick lookups and insertions with no need for sorting.
Advertisements