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.

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:

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:

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.


One 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++

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.