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.

1 thought on “C++ STL Multiset Container – std::multiset”

  1. A.S Gagan Ganapathy

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    long long int n, i, x;
    cin>>n;
    multiset s;
    multiset :: iterator it, l, r;
    for(i=1;i>x;
    s.insert(x);
    if(s.size()%2 == 1) {
    it = s.begin();
    it = it + (s.size()/2);
    cout<<(*it)<<endl;
    }
    else {
    l = s.begin();
    l = l + ((s.size()-1)/2);
    r = s.begin();
    r = r + (s.size()/2);
    long long int median = (*l + *r)/2;
    cout<<median<<endl;
    }
    }
    return 0;
    }

    here the part where I try to access the middle element of the set shows and error, is there not Any way of accessing any particular element in a multiset or a set in c++

Leave a Comment

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