C++ STL Unordered Multiset – std::unordered_multiset

In this instructional exercise you will find out about STL Unordered Multiset holder in c++ and all capacities appropriate on it.

As the name says that, this is an unordered holder. Set, unordered_set, multiset has a few limitations to store the components. Yet, here in unordered_multiset, the highlights are:

1) Elements need not pursue the explicit request. They pursue any request to store.

2) We realize that unordered compartments utilize a hash table for quick get to. This additionally utilizes a hash table to store components.

3) A facility that both esteem and key same.

4) This powerfully handles the capacity needs (for example on the fly).

5) Unlike unordered sets, here copy esteems are permitted. It just checks what number of such components are there. By making additional room.

6) Using iterator positions no one but we can erase components.

C++ STL Unordered Multiset – std::unordered_multiset

Iterators take a shot at unordered multiset:

start(): returns iterator to the start

end(): returns iterator as far as possible of the rundown

cbegin(): Returns consistent iterator to starting.

cend(): Returns steady iterator to end.

To work with Unordered multiset we have to incorporate unordered_set library.

#include<unordered_set>

Affirmation:

unordered_multiset name;

How about we see a few activities on unordered_multiset

embed(): We can utilize UMSname.insert (component), to embed. This will embed component at the front of the unordered multiset. Or on the other hand, we can straightforwardly instate the qualities utilizing props {}.

size(): This gives the number of components at present in the unordered multiset.

max_size(): This will give the limit of the unordered multiset. That what number of components in most extreme we can embed.

clear(): Clear activity will evacuate all components in the unordered multiset and void it.

void(): This is Boolean work. That profits True (1) if unordered multiset is unfilled. Generally returns False (0).

Model program to exhibit the above capacities:

#include <iostream>
#include <iterator>
#include <unordered_set>
 
using namespace std;
 
int main(){
	unordered_multiset<int> ums;
	unordered_multiset<int> :: iterator it;
	ums = {1,2,3,4,5}; // directly intializing
	ums.insert(100); // inserting a single element.
	
	cout << "Elements in the unordered multiset are" << endl;
	for(it= ums.begin(); it!=ums.end(); ++it) {
		cout << *it << " "; // printing elements in unordered multiset;
	}
	
	cout << endl;
	cout << "Size of the unordered multiset is " << ums.size() << endl;
	cout << "Maximum size of the unordered multiset is " << ums.max_size() << endl;
	
	// clear opeartin make unordered multiset empty.
	cout << "Applying clear() opeartion....." << endl;
	ums.clear();
	cout << "Checking unordered multiset is empty or not" << endl;
	
	bool flag= ums.empty();
	if (flag ==1)
		cout << "Unordered multiset is empty " << endl;
	else
		cout << "Unordered multiset is not empty " << endl;
	
	return 0;
}

Output

Elements in the unordered multiset are
5 4 3 100 2 1
Size of the unordered multiset is 6
Maximum size of the unordered multiset is 1152921504606846975
Applying clear() opeartion…..
Checking unordered multiset is empty or not
Unordered multiset is empty

find(): If we apply find() on unordered multiset like this ums.find(element). On the off chance that component is available, it will restore an iterator indicating that position. Else it will return iterator indicating the end of the unordered multiset. This will be valuable to identify climate component is available or not.

check(): If we apply count(element) on unordered multiset, it will restore a number, meaning that how often component present in the unordered multiset.

Model program:

#include <iostream>
#include <iterator>
#include <unordered_set>
 
using namespace std;
 
int main(){
	unordered_multiset<int> ums;
	unordered_multiset<int> :: iterator it;
	ums = {1,2,3,4,4,4,5}; // directly intializing
	ums.insert(100); // inserting a single element.
	
	it= ums.find(3);
	if (it != ums.end()) {
		cout << "Item  present " << endl;
	}
	else {
		cout << "Item Not present " << endl;
	}
	
	return 0;
}

Output

Item present

delete(): Erase should be possible in various ways.

Delete straightforwardly component: erase(element)

Delete by indicating the iterator specific component position.

Eradicate scope of qualities by giving start and completion pointers.

swap(): This capacity takes a shot at two unordered multisets. It moves all components of first unordered multiset to second. What’s more, move all components of the second unordered multiset to first one.

Model program to show swap and delete activities:

#include <iostream>
#include <iterator>
 
#include <unordered_set>
 
using namespace std;
int main(){
	unordered_multiset<int> ums1;
	unordered_multiset<int> ums2;
	unordered_multiset<int> :: iterator it;
	ums1 = {9, 8, 7, 6, 6, 6, 5, 4, 3};
	ums2 = {99, 88, 77, 66, 55};
	
	cout << "Elements in ums1 before swap are" << endl;
	for(it= ums1.begin(); it!=ums1.end(); ++it) {
		cout << *it << " "; 
	}
	cout << endl;
	
	cout << "Elements in the ums2 before swap are" << endl;
	for(it= ums2.begin(); it!=ums2.end(); ++it) {
		cout << *it << " "; 
	}
	cout << endl;
	
	cout << "Doing swap opeartion......." << endl;
	ums1.swap(ums2);
	cout << "Elements in ums1 after swap are" << endl;
	for(it= ums1.begin(); it!=ums1.end(); ++it) {
		cout << *it << " "; 
	}
	cout << endl;
	
	cout << "Elements in the ums2 after swap are" << endl;
	for(it= ums2.begin(); it!=ums2.end(); ++it) {
		cout << *it << " "; 
	}
	cout << endl;
	
	cout << "Erasing element 5 in ums2..." << endl;
	ums2.erase(5);
	cout << "Now erasing 1st element in ums2 by pointing iterator to it..." << endl;
	it= ums2.begin();
	ums2.erase(it);
	cout << "Range erasing...[from: where element 6 found, To:End" << endl;
	ums2.erase (ums2.find(6), ums2.end());
	
	cout << "Elements in ums2 after doing above erase operations are" << endl;
	for(it= ums2.begin(); it!=ums2.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
	
	return 0;
}

Output

Elements in ums1 before swap are
3 4 5 6 6 6 7 8 9
Elements in the ums2 before swap are
55 66 77 88 99
Doing swap opeartion…….
Elements in ums1 after swap are
55 66 77 88 99
Elements in the ums2 after swap are
3 4 5 6 6 6 7 8 9
Erasing element 5 in ums2…
Now erasing 1st element in ums2 by pointing iterator to it…
Range erasing…[from: where element 6 found, To:End
Elements in ums2 after doing above erase operations are
4

Leave a Comment

error: Alert: Content is protected!!