Skip to main content

C++ STL Multiset Container – std::multiset

In this tutorial you will learn about STL Multiset container in C++ i.e. std::multiset and all functions applicable on it.

Multiset is an associative container. Same like set this also follows some specific order to store elements. But only difference is these multisets allow duplicate values. And some more similarity to sets to multisets are both has the property that, value stored and corresponding key both are same. Elements in multiset are constant. We unable to modify after insertion of the element. If we want to update element then we should delete that element and again insert with updated element. The elements in the multiset are always sorted.

C++ STL Multiset Container – std::multiset

Iterators work on multiset:

begin(): returns iterator to the beginning.

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

rbegin(): returns reverse iterator to reverse beginning.

rend(): returns reverse iterator to reverse end.

cbegin(): Returns constant iterator to beginning.

cend(): Returns constant iterator to end.

To work with multiset we need to include set library.

#include<set>

Let’s see some operations on multiset:

1) insert(element): This operation inserts new element in the multiset. Here whatever the order we insert, those elements always inserted in a sorted order to multiset.

2) size(): This returns the number which represents current number of elements in the multiset.

3) max_size(): This function returns how many number of elements in maximum we can insert into multiset.

4) empty(): This is a Boolean operation. Returns 1 is multiset is empty. Otherwise returns 0.

5) multiset <dataType> newMultiset (oldMultiset.beginIterator(), oldMultiset.EndIterator()): This function copies all elements of existing multiset from given iterator position to given iterator end position, to a new multiset

For more clearly understanding of above functions/operations, see the below program.

#include <iostream>
#include <set>
#include <iterator>

using namespace std;

int main()
{
        multiset <int> mset; // declaring a multiset
    multiset <int> ::iterator it;
        
        for (int i=0; i<5; i++) {
                mset.insert (i+1); 
        }
        mset.insert (5); // here 5 is inserting again in the multiset. This is allowed here unlike set
        
        // showing elements in multiset
        cout << "The elements in the multiset are" << endl;
        for(it= mset.begin(); it!= mset.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;

        // size of multiset
        cout << "Size of the multiset is " << mset.size() << endl;
        
        // maximum size
        cout << "Maximum size of the multiset is " << mset.max_size() << endl;
        
        bool chk = mset.empty();
        if (chk==1) {
                cout << "Multiset is empty" << endl;
        }
        else{
                cout << "Multiset is not empty" << endl;
        }
        
    // assigning elements of the mset to other multiset
    multiset <int> mset2(mset.begin(), mset.end());

    // showing elements of the copied multiset
    cout << "Elements of new multiset after copying from mset" << endl;
    for (it = mset2.begin(); it != mset2.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
        
        return 0;
}

Output

The elements in the multiset are
1 2 3 4 5 5
Size of the multiset is 6
Maximum size of the multiset is 461168601842738790
Multiset is not empty
Elements of new multiset after copying from mset
1 2 3 4 5 5

Some other modifying operations on multiset are:

1) erase(element): It removes the specified element from multiset .We can also delete a range of elements by using iterators.

2) swap(): If there are two multisets m1 and m2. Both need not be equal size. Swap operation swaps all elements of m2 to m1. And also from m1 to m2.

3) clear(): This removes all elements from multiset. So results multiset size 0.

Example program to understand above functions:

#include <iostream>
#include <set>
#include <iterator>

using namespace std;

int main()
{
        multiset <int> mset1;
        multiset <int> mset2;
        multiset <int> ::iterator it;
        
        for (int i=0; i<5; i++) {
                mset1.insert (i+1); 
        }
        
        // erase operation
        mset1.erase(5);
        cout << "Elements after removing 5" << endl;
        for (it=mset1.begin(); it!=mset1.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;
        
        // inserting elements to mset2
        for (int i=0;i<4;i++) {
                mset2.insert(i+100);
        }
        cout << "elements of multiset2 are" << endl;
        for (it=mset2.begin(); it!= mset2.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;
        
        cout << "performing swap operation on entire sets..." << endl;
        mset1.swap(mset2);
        cout << "Elements of multiset1 after swap operation" << endl;
        for (it=mset1.begin(); it!=mset1.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;
        
        cout << "Elements of multiset2 after swap operation" << endl;
        for (it=mset2.begin(); it!=mset2.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;
        
        cout << "Applying clear operation on multiset2..." << endl;
        mset2.clear();
        bool chk= mset2.empty();
        if (chk==1) {
                cout << "Multiset2 is empty" << endl;
        }
        else {
                cout << "Multiset2 is not empty" << endl;
        }
        
        return 0;
}

Output

Elements after removing 5
1 2 3 4
elements of multiset2 are
100 101 102 103
performing swap operation on entire sets…
Elements of multiset1 after swap operation
100 101 102 103
Elements of multiset2 after swap operation
1 2 3 4
Applying clear operation on multiset2…
Multiset2 is empty

Some more operations are:

1) find (element): This operation used to find the particular element form multiset. After finding using iterator we can delete that element or we can print.

2) count (element): This operation used to count how many number of times element occurs in the multiset.

3) lower_bound (element): This returns iterator at specified element position.

4) upper_bound (element): This returns iterator at specified element position.

Using upper bound and lower bound we can perform operations on range of elements between them.

Example program to explain about above operations:

#include <iostream>
#include <set>
#include <iterator>

using namespace std;

int main()
{
        multiset <int> mset1;
        multiset <int> ::iterator it;

        for (int i=0; i<5; i++) {
                mset1.insert (i+1); 
        }
        it= mset1.find(3); // finding element 3
        cout << "Element found is " << *it << endl;
        
        // for count operation I am adding some more elements of 2
        mset1.insert (2);
        mset1.insert (2);
        
        cout << "In multiset element 2 occured " << mset1.count(2) << " times" << endl;
        
        multiset <int> :: iterator lob, upb;
        lob= mset1.lower_bound (2);
        upb= mset1.upper_bound (4);

        cout << "Removing elements form lower bound to upper bound..." << endl;
        mset1.erase (lob,upb);
        cout << "After removing elements are" << endl;
        for (it=mset1.begin(); it!=mset1.end(); it++) {
                cout << *it << " ";
        }
        cout << endl;
        
        return 0;
}

Output

Element found is 3
In multiset element 2 occured 3 times
Removing elements form lower bound to upper bound…
After removing elements are
1 5

Comment below if you have any queries related to above C++ stl multiset or std::multiset tutorial.

The post C++ STL Multiset Container – std::multiset appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/10/stl-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

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

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do