Table of contents
1.
Introduction
2.
Internal Implementation of unordered_multiset in C++
3.
Functions in unordered_multiset in C++
4.
Example
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Unordered_multiset in C++

Author Muskan Gupta
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Unordered associative containers are divided into unordered_maps, unordered_sets, unordered_multiset, and unordered_multimap. In unordered_set or unordered_map, unique elements are allowed. On the other hand, unordered_multiset in C++ is a type of unordered associative container that can have duplicate elements, and it is very similar to the unordered_set data structure. 

                                                                                Unordered Associative containers

This article covers the concept of unordered_multiset in C++, its methods, and its use.

Internal Implementation of unordered_multiset in C++

The internal implementation of unordered_multiset in C++ is almost the same. The difference is unordered_set uses the hash table to search, and only the value count is associated with each value in the unordered_multiset in C++. Due to the hashing of elements, it does not have a specific order of storage of elements so that every element can come in any way, but the duplicate part comes together. Every operation for the unordered_multiset in C++ takes constant time on an average time but in some cases can rise to the linear in the worst-case scenario.

Functions in unordered_multiset in C++

Following are the functions present to use in STL unordered_miltiset.

Function

Use

insert() It inserts a new element, increasing the size of the unordered_multiset.
empty() empty() is used to check whether the unordered_multiset is empty or not.
begin() It returns an iterator that points to the first element in the unordered_multiset or the first element in one of the buckets present.
end() It returns an iterator that points to just after the last element in the unordered_multiset or just after the last element in one of the buckets present.
find(val) It is used to find out whether a particular element, Val is present in the unordered_multiset or not.
equal_range() It returns the range in which every element is equal to a given element's value.
emplace() It is used to insert a new element.
size() It tells the current size of unordered_multiset.
clear() It clears the contents present in the unordered_multiset.
count(val)

It returns the number of occurrences of the particular element,val.

 

max_size() It returns the maximum number of elements that the unordered_multiset can contain.
swap() It is used to swap the contents present in two unordered_multiset.
bucket() It returns the bucket's number in which a particular element is present. The bucket size belongs to the range 0 to bucket_count - 1.
bucket_count() It is used for returning the number of buckets in the unordered_multiset.
bucket_size(val) It is used to return the size of the bucket which has a particular element, val.
max_bucket_count() It returns the maximum number of buckets that the unordered_multiset can contain.
erase() It works in two ways. We can remove either one element from the unordered_multiset or all elements with a particular value belonging to a particular range(start-Inclusive and end-exclusive).
load_factor() It is used to return the current load factor value in the unordered_multiset.
max_load_factor() It returns the load factor’s maximum value of the unordered_multiset.

Example

This program depicts how unordered_multiset in C++ is declared and how its functions are used. Functions such as empty(), find(val), size() and count() has been used in the example shown below.

Code:

// Program to demonstrate different functions of unordered_multiset in //C++
#include <bits/stdc++.h>
using namespace std;

// typedef for declaring iterator
typedef unordered_multiset<int>::iterator umit;

// Function for printing unordered_multiset
void DisplayUms(unordered_multiset<int> umts)
{
	// begin() returns iterator to first element of unordered_multiset
	umit it;
	for (it = umts.begin(); it != umts.end(); it++)
		cout << *it << " ";
	cout << "\n";
}

// Program to check all functions
int main()
{
	// Empty initialization
	unordered_multiset<int> umset1;

    // Initialization by assignment
	umset1 = {2, 4, 5, 2, 0, 3, 4, 5};

	// Initialization by intializer list
	unordered_multiset<int> umset2 ({2, 3, 2, 7, 2, 3, 4, 2, 6});

	// empty() returns true if unordered_multiset is empty
	if (umset1.empty())
		cout << "1st unordered multiset is empty\n";
	else
		cout << "1st unordered multiset is not empty\n";
        // Otherwise false
         
	// size() returns number of elements present in data structure
	cout << "The size of second unordered multiset  is : "<< umset2.size() << endl;
    int val = 3;
    cout<<"Printing 1st unordered mutiset"<<endl;
	DisplayUms(umset1);

	umset1.insert(7);
    cout<<"Printing 1st unordered mutiset after inserting the element 7"<<endl;
	DisplayUms(umset1);

	/* Find is returning the iterator to first position of val, if exists otherwise it returns iterator
	to end*/
	if (umset1.find(val) != umset1.end())
		cout << "1st unordered multiset contains "<< val << endl;
	else
		cout << "1st unordered multiset does not contains "<< val << endl;

	// Count function is returning total number of occurrences in set
	val = 5;
	int cnt = umset1.count(val);
	cout << val << " occurs " << cnt << " times in unordered multiset 1 \n";

	val = 9;

	// If count return greater than 0 value then element exists otherwise not
	if (umset1.count(val))
		cout << "1st unordered multiset contains "<< val << endl;
	else
		cout << "1st unordered multiset does not contains "<< val << endl;

	val = 1;

	/* Equal_range returns a pair,where first is iterator to first position of val and second is iterator to last position to val*/
	pair<umit, umit> enrange_it = umset2.equal_range(val);
	if (enrange_it.first != enrange_it.second)
		cout << val << " occured atleast once in unoredered_multiset \n";

    cout<<"Printing 2nd unordered mutiset"<<endl;
	DisplayUms(umset2);

	// Erase function deletes all instances of val
	umset2.erase(val);
    cout<<"Printing 2nd unordered mutiset after calling erase function for the element 1"<<endl;
	DisplayUms(umset2);

	// Clear function deletes all entries from set
	umset1.clear();
	umset2.clear();

	if (umset1.empty())
		cout << "1st unordered multiset is empty\n";
	else
		cout << "1st unordered multiset is not empty\n";
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

1st unordered multiset is not empty
The size of second unordered multiset  is : 9
Printing 1st unordered mutiset
3 0 5 5 4 4 2 2 
Printing 1st unordered mutiset after inserting the element 7
7 3 0 5 5 4 4 2 2 
1st unordered multiset contains 3
5 occurs 2 times in unordered multiset 1 
1st unordered multiset does not contains 9
Printing 2nd unordered mutiset
6 4 7 3 3 2 2 2 2 
Printing 2nd unordered mutiset after calling erase function for the element 1
6 4 7 3 3 2 2 2 2 
1st unordered multiset is empty

 

Here, we can see that most of the functions used in this program are similar to unordered_set. But there are some different functions as well in unordered_multiset. Let's look at the list of functions of unordered_multiset in the next section.

Check out this problem - First And Last Occurrences Of X

Must Read Dynamic Binding in C++

FAQs

  1. What is hash_function() in unordered_multiset?
    It is a unary function that accepts a single parameter and returns a unique value having type size_t.
     
  2. What is rehash() in unordered_multiset?
    It is used for setting the number of buckets to N or more in the container.
     
  3. What is the difference between multiset and vector?
    A multiset is slower to insert any object than a vector, but they are still sorted. But multiset takes more space because of the pointer used in the inner implementation.
     
  4. What is reserve() in unordered_multiset?
    It is used for setting the number of buckets in the container or bucket_count such that it at least contains n elements.
     
  5. What is operator ‘=’ in unordered_multiset?
    '=' is an operator used for C++ STL to copy or move one unordered_multiset to another unordered_multiset, and the operator function for this purpose is unordered_multiset::operator=.

Key Takeaways

In this article, we have extensively discussed the concepts of Unordered_multiset its functions, along with an example and frequently asked questions. 

We hope that this blog has helped you enhance your knowledge regarding Unordered_multiset and if you would like to learn more, check out our articles on STL containers in c++. Do upvote our blog to help other ninjas grow. Happy Coding!
 

Live masterclass