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:
#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). 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:
#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
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:
#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
The post C++ STL Unordered Multiset – std::unordered_multiset appeared first on The Crazy Programmer.
from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/12/stl-unordered-multiset.html
Comments
Post a Comment