C++ Program to Implement Unordered_multiset in STL

«
»
This C++ Program demonstrates the implementation of Unordered_multisetin STL.

Here is source code of the C++ Program to implement Unordered_multisetin 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 Unordered_multiset in STL
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. #include <cstdlib>
  7. #include <unordered_set>
  8. using namespace std;
  9. int main()
  10. {
  11.     unordered_multiset<string> myset = {"red","green","blue"};
  12.     unordered_multiset<string>::const_iterator got;
  13.     unordered_multiset<string>::iterator it;
  14.     int choice, item;
  15.     string s;
  16.     while (1)
  17.     {
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"Unordered MultiSet Implementation in Stl"<<endl;
  20.         cout<<"\n---------------------"<<endl;
  21.         cout<<"1.Insert Element"<<endl;
  22.         cout<<"2.Delete Element"<<endl;
  23.         cout<<"3.Find Element"<<endl;
  24.         cout<<"4.Count Elements"<<endl;
  25.         cout<<"5.Size of the Unordered Set"<<endl;
  26.         cout<<"6.Count number of Buckets"<<endl;
  27.         cout<<"7.Buckets"<<endl;
  28.         cout<<"8.Display Unordered Set"<<endl;
  29.         cout<<"9.Exit"<<endl;
  30.         cout<<"Enter your Choice: ";
  31.         cin>>choice;
  32.         switch(choice)
  33.         {
  34.         case 1:
  35.             cout<<"Enter string to be inserted: ";
  36.             cin>>s;
  37.             myset.insert(s);
  38.             break;
  39.         case 2:
  40.             cout<<"Enter string to be deleted: ";
  41.             cin>>s;
  42.             myset.erase(s);
  43.             break;
  44.         case 3:
  45.             cout<<"Enter key string to find ";
  46.             cin>>s;
  47.             got = myset.find (s);
  48.             if (got == myset.end())
  49.                 cout<<"Element found in myset"<<endl;
  50.             else
  51.                 cout<<"Element not found in myset"<<endl;
  52.             break;
  53.         case 4:
  54.             cout<<"Enter string to be counted: ";
  55.             cin>>s;
  56.             cout<<"There are "<<myset.count(s)<<" elements with '"<<s<<"'\n";
  57.             break;
  58.         case 5:
  59.             cout<<"Size of the Unordered MultiSet: "<<myset.size()<<endl;
  60.             break;
  61.         case 6:
  62.             cout<<"myset has "<< myset.bucket_count()<<" buckets.\n";
  63.             break;
  64.         case 7:
  65.             for (const string& x: myset)
  66.             {
  67.                 cout<< x <<" is in bucket #" << myset.bucket(x) << endl;
  68.             }
  69.             break;
  70.         case 8:
  71.             cout<<"Elements of the Unordered MultiSet:  ";
  72.             for (it = myset.begin(); it != myset.end(); ++it)
  73.                 cout << " " << *it;
  74.             cout<<endl;
  75.             break;
  76. 	case 9:
  77.             exit(1);
  78. 	    break;
  79.         default:
  80.             cout<<"Wrong Choice"<<endl;
  81.         }
  82.     }
  83.     return 0;
  84. }

advertisement
$ 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
 
------------------
(program exited with code: 1)
Press return to continue

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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn