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...

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...