C++ STL’s unordered_multiset And Its Applications

In this tutorial, we shall learn about C++ STL’s unordered_multiset and its applications. For understanding this you must know unordered_set which you can read it here unordered_set. But there is a problem in unordered_set, it is not possible to store duplicate entries. For example, if we enter the same value say k twice in an unordered_set then it would simply get ignored.

So the unordered_multiset comes as a savior in this problem. It can store duplicate values. It is done using incrementing a count variable internally which is associated with each value. But it comes at cost of space i.e. the if we add distinct elements to unordered_set and unordered_multiset then the latter will consume more space than the former due to the extra count variable associated with it.

unordered_multiset Functions

It has the following functions :

  • begin()–Returns an iterator pointing to the first element in the container or pointing to the first element in one of its bucket
  • end()–Returns an iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket
  • insert()–Inserts new elements in the unordered_multiset and increases the container size
  • equal_range()– Returns the range in which all the elements are equal to a given value

There are many more functions associated with unordered_multiset like erase(), max_size(), reserve() etc. Now let’s see an example code:

#include <bits/stdc++.h>
using namespace std;
typedef unordered_multiset<int>::iterator umit; // macro for unordered_multiset<int>::iterator
void PrintUtility(unordered_multiset<int>);
// Function to print unordered_multiset
int main()
{
    unordered_multiset<int> umsl1; // empty initialization

    unordered_multiset<int> umsl2({1, 2, 1, 5, 9, 4,
                                   4, 8, 6}); // initializer list

    umsl1 = {0, 5, 2, 7, 0, 4, 7, 5}; //  Initialization by assignment

    if (umsl1.empty()) //  empty() function return true if the set is empty else false
        cout << "The unordered_multiset first is empty\n\n";
    else
        cout << "The unordered_multiset first is not empty\n\n";

    cout << "The size of unordered_multiset second is : "
         << umsl2.size() << endl; //  size() function returns total number of elements

    PrintUtility(umsl1);

    umsl1.insert(8);

    PrintUtility(umsl1);

    int val = 3;
    if (umsl1.find(val) != umsl1.end())
        cout << "The unordered_multiset first contains "
             << val << endl;
    else
        cout << "The unordered_multiset first does not contains "
             << val << endl;

    val = 5;
    int cnt = umsl1.count(val); //  count function returns total number of occurrence in set
    cout << val << " appears " << cnt
         << " times in unordered_multiset first \n\n";

    val = 9;

    if (umsl1.count(val)) //  if count return >0 value then element exist otherwise it doesn't
        cout << "The unordered_multiset first contains "
             << val << endl;
    else
        cout << "The unordered_multiset first does not contains "
             << val << endl;

    val = 1;

    pair<umit, umit> erange_it = umsl2.equal_range(val);
    if (erange_it.first != erange_it.second)
        cout << val << " appeared atleast once in "
                       "unoredered_multiset \n\n";

    PrintUtility(umsl2);

    // erase function deletes all instances of val
    umsl2.erase(val);

    PrintUtility(umsl2);

    // clear function deletes all entries from set
    umsl1.clear();
    umsl2.clear();

    if (umsl1.empty())
        cout << "The unordered_multiset first is empty\n\n";
    else
        cout << "The unordered_multiset first is not empty\n\n";
}
void PrintUtility(unordered_multiset<int> umls)
{
    umit it = umls.begin(); //  begin() returns iterator to first element of set
    for (; it != umls.end(); it++)
        cout << *it << " ";
    cout << endl;
}

The output will be:

The unordered_multiset first is not empty                                                                                              

                                                                                                                                       

The size of unordered_multiset second is : 9                                                                                           

4 7 7 2 5 5 0 0                                                                                                                        

8 4 7 7 2 5 5 0 0                                                                                                                      

The unordered_multiset first does not contains 3                                                                                       

5 appears 2 times in unordered_multiset first                                                                                          

                                                                                                                                       

The unordered_multiset first does not contains 9                                                                                       

1 appeared atleast once in unoredered_multiset                                                                                         

                                                                                                                                       

6 8 4 4 9 5 2 1 1                                                                                                                      

6 8 4 4 9 5 2                                                                                                                          

The unordered_multiset first is empty

You may also like to learn:

Leave a Reply

Your email address will not be published. Required fields are marked *