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.
/*
* C++ Program to Implement Multiset in Stl
*/
#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
multiset<int> ms;
multiset<int>::iterator it, it1;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Multiset Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Multiset"<<endl;
cout<<"2.Delete Element from the Multiset"<<endl;
cout<<"3.Find Element in a Multiset"<<endl;
cout<<"4.Count Elements with a specific key"<<endl;
cout<<"5.Size of the Multiset"<<endl;
cout<<"6.Display Multiset"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
if (ms.empty())
it1 = ms.insert(item);
else
it1 = ms.insert(it1, item);
break;
case 2:
cout<<"Enter value to be deleted: ";
cin>>item;
ms.erase(item);
break;
case 3:
cout<<"Enter element to find ";
cin>>item;
it = ms.find(item);
if (it != ms.end())
cout<<"Element found"<<endl;
else
cout<<"Element not found"<<endl;
break;
case 4:
cout<<"Enter element to be counted: ";
cin>>item;
cout<<item<<" appears "<<ms.count(item)<<" times."<<endl;
break;
case 5:
cout<<"Size of the Multiset: "<<ms.size()<<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;
}
$ 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.
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Data Structure Books
- Practice Design & Analysis of Algorithms MCQ
- Practice Computer Science MCQs