C++ STL Unordered Multiset – std::unordered_multiset

In this tutorial you will learn about STL Unordered Multiset container in c++ and all functions applicable on it.

As name says that, this is an unordered container. Set, unordered_set, multiset has some restrictions to store the elements. But here in unordered_multiset, the features are:

1) Elements need not follow specific order. They follow any order to store.

2) We know that unordered containers uses hash table for fast access. This also uses hash table to store elements.

3) Facility that both value and key same.

4) This dynamically handles the storage needs (i.e. on fly).

5) Unlike unordered sets, here duplicate values are allowed. It just counts how many such elements are there. By creating extra space.

6) Using iterator positions only we can delete elements.

C++ STL Unordered Multiset – std::unordered_multiset

Iterators work on unordered multiset:

begin(): returns iterator to the beginning

end(): returns iterator to the end of the list

cbegin(): Returns constant iterator to beginning.

cend(): Returns constant iterator to end.

To work with Unordred multiset we need to include unordered_set library.

#include<unordered_set>

Declaration:

unordered_multiset <data_type> name; 

Let’s see some operations on unordered_multiset

insert(): We can use UMSname.insert (element),  to insert. This will insert element at the front of the unordered multiset. Or we can directly initialize the values using braces {}.

size(): This gives the number of elements currently present in the unordered multiset.

max_size(): This will give the capacity of the unordered multiset. That how many elements in maximum we can insert.

clear(): Clear operation will removes all elements in the unordered multiset and empty it.

empty(): This is Boolean function. That returns True (1) if unordered multiset is empty. Otherwise returns False (0).

Example program to demonstrate above functions:

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). If element is present, it will return an iterator pointing to that position. Otherwise it will return iterator pointing to end of the unordered multiset. This will be useful to detect weather element is present or not.

count(): If we apply count(element) on unordered multiset, it will return an integer, denoting that how many times element present in the unordered multiset.

Example program:

Output

Item present

erase(): Erase can be done in different ways.

  • Erase directly element: erase(element)
  • Erase by pointing the iterator to particular element position.
  • Erase range of values by giving beginning and ending pointers.

swap(): This function works on two unordered multisets. It move all elements of 1st unordered multiset to 2nd. And move all elements of 2nd unordered multiset to 1st one.

Example program to show swap and erase operations:

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

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