C++ Program to Implement Multiset in STL

C++Server Side ProgrammingProgramming

A multiset is a type of associative container in which has multiple elements can have same values.

Functions and descriptions:

Functions are used here:
   ms.size() = Returns the size of multiset.
   ms.insert) = It is used to insert elements to the multiset.
   ms.erase() = Removes the value from the multiset.
   ms.find() = Returns an iterator to the search element in the multiset if found,
   else returns the iterator to end.
   ms.count() = Returns the number of matches element in the multiset.
   ms.begin() = Returns an iterator to the first element in the multiset.
   ms.end() = Returns an iterator to the last element in the multiset

Example Code

#include<iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
   multiset<int> ms;
   multiset<int>::iterator it, it1;
   int c, i;
   while (1) {
      cout<<"1.Size of the Multiset"<<endl;
      cout<<"2.Insert Element into the Multiset"<<endl;
      cout<<"3.Delete Element from the Multiset"<<endl;
      cout<<"4.Find Element in a Multiset"<<endl;
      cout<<"5.Count Elements with a specific key"<<endl;
      cout<<"6.Display Multiset"<<endl;
      cout<<"7.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of the Multiset: "<<ms.size()<<endl;
         break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            if (ms.empty())
               it1 = ms.insert(i);
            else
               it1 = ms.insert(it1, i);
         break;
         case 3:
            cout<<"Enter value to be deleted: ";
            cin>>i;
            ms.erase(i);
         break;
         case 4:
            cout<<"Enter element to find ";
            cin>>i;
            it = ms.find(i);
            if (it != ms.end())
               cout<<"Element found"<<endl;
            else
               cout<<"Element not found"<<endl;
         break;
         case 5:
            cout<<"Enter element to be counted: ";
            cin>>i;
            cout<<i<<" appears "<<ms.count(i)<<" times."<<endl;
         break;
         case 6:
            cout<<"Elements of the Multiset: ";
            for (it = ms.begin(); it != ms.end(); it++)
               cout<<*it<<" ";
            cout<<endl;
         break;
         case 7:
            exit(1);
         break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

Output

1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 1
Size of the Multiset: 0
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 3
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 6
Elements of the Multiset: 1 2 3 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 3
Enter value to be deleted: 4
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 4
Enter element to find 1
Element found
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 5
Enter element to be counted: 2
2 appears 1 times.
1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 7
Exit code: 1
raja
Published on 29-Mar-2019 11:55:54
Advertisements