Skip to main content

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:

#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

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...