C++ Program to Implement Unordered_multiset in STL
#include <iostream>
#include <string>
#include <cstdlib>
#include <unordered_set>
using namespace std;
int main()
{
unordered_multiset<string> myset = {"red","green","blue"};
unordered_multiset<string>::const_iterator got;
unordered_multiset<string>::iterator it;
int choice, item;
string s;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Unordered MultiSet Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element"<<endl;
cout<<"2.Delete Element"<<endl;
cout<<"3.Find Element"<<endl;
cout<<"4.Count Elements"<<endl;
cout<<"5.Size of the Unordered Set"<<endl;
cout<<"6.Count number of Buckets"<<endl;
cout<<"7.Buckets"<<endl;
cout<<"8.Display Unordered Set"<<endl;
cout<<"9.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter string to be inserted: ";
cin>>s;
myset.insert(s);
break;
case 2:
cout<<"Enter string to be deleted: ";
cin>>s;
myset.erase(s);
break;
case 3:
cout<<"Enter key string to find ";
cin>>s;
got = myset.find (s);
if (got == myset.end())
cout<<"Element found in myset"<<endl;
else
cout<<"Element not found in myset"<<endl;
break;
case 4:
cout<<"Enter string to be counted: ";
cin>>s;
cout<<"There are "<<myset.count(s)<<" elements with '"<<s<<"'\n";
break;
case 5:
cout<<"Size of the Unordered MultiSet: "<<myset.size()<<endl;
break;
case 6:
cout<<"myset has "<< myset.bucket_count()<<" buckets.\n";
break;
case 7:
for (const string& x: myset)
{
cout<< x <<" is in bucket #" << myset.bucket(x) << endl;
}
break;
case 8:
cout<<"Elements of the Unordered MultiSet: ";
for (it = myset.begin(); it != myset.end(); ++it)
cout << " " << *it;
cout<<endl;
break;
case 9:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
Output:
$ g++ unorderedmultiset_stl.cpp
$ a.out
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 1
Enter string to be inserted: black
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 1
Enter string to be inserted: white
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 1
Enter string to be inserted: red
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 5
Size of the Unordered MultiSet: 6
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 8
Elements of the Unordered MultiSet: white black blue green red red
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 2
Enter string to be deleted: red
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 5
Size of the Unordered MultiSet: 4
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 7
white is in bucket #1
black is in bucket #6
blue is in bucket #4
green is in bucket #2
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 6
myset has 7 buckets.
---------------------
Unordered MultiSet Implementation in Stl
---------------------
1.Insert Element
2.Delete Element
3.Find Element
4.Count Elements
5.Size of the Unordered Set
6.Count number of Buckets
7.Buckets
8.Display Unordered Set
9.Exit
Enter your Choice: 9
