How to remove specific elements from multiset in C++
In this tutorial, we will see how to remove elements from multiset in C++. But first, we see what exactly multiset is and how it is similar to a set in STL. So multiset is an associative container in C++ that stores elements in a sorted manner but there is a slight difference between a set and multiset. In set elements are stored in a sorted manner and it allows a single unique element only. In multiset elements are stored in a sorted manner and it allows unique as well as repeated elements. So in this tutorial, we will see how can we remove some specific elements from a multiset through some examples.
Remove specific elements present in an array
#include<bits/stdc++.h> using namespace std; int main(){ multiset<int>m = {7,6,4,5,2,3,1,1,2,4,5}; int arr[3] = {5,4,2}; sort(arr, arr+3); cout<<"Elements in a multiset before remove elements present in an array are: "<<endl; for(auto &x:m){ cout<<x<<" "; } cout<<endl<<endl; int size1 = m.size(); int size2 = 3; int i = 0; int j = 0; while((i < size1) && (j<size2)){ if(arr[j] == arr[i]){ m.erase(arr[i]); i++; } if(arr[j] < arr[i]){ j++; } else{ i++; } } cout<<"Elements in a multiset after removed elements present in an array are: "<<endl; for(auto& x:m){ cout<<x<<" "; } cout<<endl; return 0; }
Output –
Elements in a multiset before remove elements present in an array are: 1 1 2 2 3 4 4 5 5 6 7 Elements in a multiset after removed elements present in an array are: 1 1 3 6 7
In the above example, our task is to remove some elements present in an array from a multiset. Multiset elements are already in a sorted manner so we don’t need to sort them but we have to sort the array elements. After sorting our task is easy if you go through the while loop you will understand the code easily.
Remove Repeated elements from multiset
#include<bits/stdc++.h> using namespace std; int main(){ multiset<int>m = {5,4,3,2,1,5,4}; multiset<int>::iterator it; for(it = m.begin();it != m.end();it++){ int num = 5; if((*it) == num){ m.erase(it); it = m.begin(); } } cout<<"Elements in a multiset after remove any one repeated elements are: "<<endl; for(it = m.begin();it != m.end();it++){ cout<<*it<<endl; } return 0; }
Output –
Elements in a multiset after remove any one repeated elements are: 1 2 3 4 4
In this example, we consider one repeated element (i.e. 5) in this case. So in the first for loop we go through each element to check if it is 5 or not. If it is 5 then remove that element and then we print that whole set by using second for loop.
Removing elements of a specific range
In this example, we will see how to remove the first 3 elements by using the erase method in multimap.
#include<bits/stdc++.h> using namespace std; int main(){ multiset<int>m = {5,4,3,2,1,5,4,6,7,8,9,10,11,12}; multiset<int>::iterator it1; multiset<int>::iterator it2; cout<<"Elements in a multiset before removing is: "<<endl; for(it1 = m.begin();it1 != m.end();it1++){ cout<<*it1<<" "; } cout<<endl; it1 = m.begin(); it2 = m.begin(); it2++; it2++; it2++; auto it3 = it1; m.erase(it1,it2); cout<<"Elements in a multiset after removing is: "<<endl; for(it3 = m.begin();it3 != m.end();it3++){ cout<<*it3<<" "; } cout<<endl; return 0; }
Output –
Elements in a multiset before removing is: 1 2 3 4 4 5 5 6 7 8 9 10 11 12 Elements in a multiset after removing is: 4 4 5 5 6 7 8 9 10 11 12
In this example, we have seen how we can remove elements of a specific range in a multiset using the erase method. Just like the above example, we can remove any range of elements easily.
So this is how we can remove some specific elements from multiset. Hope this tutorial will be helpful.
Leave a Reply