C++ Program to Implement Multiset in STL

This C++ Program demonstrates implementation of Multiset in STL.

Here is source code of the C++ Program to demonstrate Multiset in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Multiset in Stl
  3.  */
  4. #include <iostream>
  5. #include <set>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     multiset<int> ms;
  12.     multiset<int>::iterator it, it1;
  13.     int choice, item;
  14.     while (1)
  15.     {
  16.         cout<<"\n---------------------"<<endl;
  17.         cout<<"Multiset Implementation in Stl"<<endl;
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"1.Insert Element into the Multiset"<<endl;
  20.         cout<<"2.Delete Element from the Multiset"<<endl;
  21.         cout<<"3.Find Element in a Multiset"<<endl;
  22.         cout<<"4.Count Elements with a specific key"<<endl;
  23.         cout<<"5.Size of the Multiset"<<endl;
  24.         cout<<"6.Display Multiset"<<endl;
  25.         cout<<"7.Exit"<<endl;
  26.         cout<<"Enter your Choice: ";
  27.         cin>>choice;
  28.         switch(choice)
  29.         {
  30.         case 1:
  31.             cout<<"Enter value to be inserted: ";
  32.             cin>>item;
  33.             if (ms.empty())
  34.                 it1 = ms.insert(item);
  35.             else
  36.                 it1 = ms.insert(it1, item);
  37.             break;
  38.         case 2:
  39.             cout<<"Enter value to be deleted: ";
  40.             cin>>item;
  41.             ms.erase(item);
  42.             break;
  43.         case 3:
  44.             cout<<"Enter element to find ";
  45.             cin>>item;
  46.             it = ms.find(item);
  47.             if (it != ms.end())
  48.                 cout<<"Element found"<<endl;
  49.             else
  50.                 cout<<"Element not found"<<endl;
  51.             break;
  52.         case 4:
  53.             cout<<"Enter element to be counted: ";
  54.             cin>>item;
  55.             cout<<item<<" appears "<<ms.count(item)<<" times."<<endl;
  56.             break;
  57.         case 5:
  58.             cout<<"Size of the Multiset: "<<ms.size()<<endl;
  59.             break;
  60.         case 6:
  61.             cout<<"Elements of the Multiset:  ";
  62.             for (it = ms.begin(); it != ms.end(); it++)
  63.                 cout<<*it<<"  ";
  64.             cout<<endl;
  65.             break;
  66. 		case 7:
  67.             exit(1);
  68. 	        break;
  69.         default:
  70.             cout<<"Wrong Choice"<<endl;
  71.         }
  72.     }
  73.     return 0;
  74. }

$ g++ multiset.cpp
$ a.out
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 100
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 200
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 300
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 400
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 500
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 5
Size of the Multiset: 5
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 6
Elements of the Multiset:  100  200  300  400  500  
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 2
Enter value to be deleted: 300
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 5
Size of the Multiset: 4
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 6
Elements of the Multiset:  100  200  400  500  
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 4
Enter element to be counted: 100
100 appears 1 times.
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 1
Enter value to be inserted: 100
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 6
Elements of the Multiset:  100  100  200  300  400  500  
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 4
Enter element to be counted: 100
100 appears 2 times.
 
---------------------
Multiset Implementation in Stl
 
---------------------
1.Insert Element into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.Exit
Enter your Choice: 7
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.