Advertisement
momo2345

multiset

Aug 13th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. //Initial Template for C++
  2. //Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. multiset<int> multisetInsert(int arr[],int n); //Function to insert elements of array into a multiset and return a multiset
  8.  
  9.  
  10. void multisetDisplay(multiset<int>s); //function to print the elements of the multiset
  11.  
  12.  
  13. void multisetErase(multiset<int>&s,int x); //function to erase x from multiset if it exists
  14.  
  15.  
  16.  // } Driver Code Ends
  17.  
  18.  
  19. //User function Template for C++
  20.  
  21.  
  22. multiset<int> multisetInsert(int arr[],int n)
  23. {
  24.     multiset<int>s;
  25.    //Insert elements into a multiset
  26.     for(int i=0;i<n;i++)
  27.     s.insert(arr[i]);
  28.    
  29.     return s;
  30.    
  31. }
  32.  
  33.  
  34. void multisetDisplay(multiset<int>s)
  35. {
  36.     //Multiset display print elements
  37.     for(auto u:s)
  38.     cout<<u<<" ";
  39.     cout<<endl;
  40. }
  41.  
  42.  
  43. void multisetErase(multiset<int>&s,int x)
  44. {
  45.    
  46.     //If and else condition to erase x from multiset
  47.    if(s.count(x)>=1){
  48.      s.erase(x);
  49.     cout<<"erased "<<x;}
  50.   else
  51.     cout<<"not found";
  52.    
  53.    
  54.     cout<<endl;
  55. }
  56.  
  57. // { Driver Code Starts.
  58.  
  59. int main() {
  60.     int t;
  61.     cin>>t;
  62.     while(t--)
  63.     {
  64.         int n;
  65.         cin>>n;
  66.         int arr[n]; //array of size n
  67.         for(int i=0;i<n;i++)
  68.         cin>>arr[i]; //Input the array
  69.        
  70.         multiset<int>s=multisetInsert(arr,n); //call the insert function that returns a multiset
  71.         multisetDisplay(s);// display the inserted multiset
  72.         int x;
  73.         cin>>x; //x element that needs to be erased from multiset
  74.        
  75.         multisetErase(s,x); //try to erase x from multiset
  76.         multisetDisplay(s); //print the multiset after erase operation
  77.        
  78.        
  79.     }
  80.     return 0;
  81. }
  82.  
  83.  
  84.  
  85.   // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement