Multiset in STL C++

About Multiset in STL :

Multiset in STL is defined as the container used to store the elements just like set but they can store them in sorted order or duplicate element can also exist in it.

multiset in stl

Working of Multiset :

In STL, Set-like containers called multisets exist. Multisets, as contrast to sets, may organise the storage of duplicate elements.Once they are added to the multiset, the items cannot be modified; they can only be added or removed.The header file for #include<set> contains a multiset. Iterators can be used to access the multiset’s elements.

Functions in Multiset :

 FunctionWorking
insert()insert an element to the multiset.
clear()remove all the element of the multiset.
size()give the number of elements present in the multiset.
empty()tells whether the set is empty or not.
count()return the frequency of particular character in the multiset.

Example of Multiset in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   multiset< int> mset;
   mset.insert(1);
   mset.insert(2);
   mset.insert(10);
   mset.insert(15);
   mset.insert(1);
   mset.insert(4);
   cout<<"The elements in the multiset : ";
   for(auto it : mset){
       cout<< it<<" ";
   }
  
    return 0;
}

Output :

The elements in the multiset : 1 1 2 4 10 15 

In the above program, we take the multiset ,mset ,insert elements by insert() and  print the elements of multiset.

Example of Multiset in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   multiset< int> mset;
   mset.insert(1);
   mset.insert(2);
   mset.insert(10);
   mset.insert(15);
   mset.insert(1);
   mset.insert(4);
   cout<<"The elements in the multiset : ";
   for(auto it : mset){
       cout<< it<<" ";
   }
  cout<< endl;
  cout<<"Element 1 is present "<< mset.count(1)<< " no. of times in the multiset"<< endl;
  cout<<"Number of elements present in the multiset : "<< mset.size();
    return 0;
}

Output :

The elements in the multiset : 1 1 2 4 10 15 
Element 1 is present 2 no. of times in the multiset
Number of elements present in the multiset : 6

In the above program, we take the multiset ,mset ,insert elements by insert() and  print the elements of multiset, used count() to find the frequency of particular element and also find the number of elements in the multiset.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription