C++ Program to Implement Unordered_set in STL

This C++ Program demonstrates the implementation of Unordered_setin STL.

Here is source code of the C++ Program to implement Unordered_setin 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_set 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_set<string> myset = {"Mercury","Venus","Earth", "Mars","Jupiter","Saturn","Uranus"};
  12.     unordered_set<string>::const_iterator got;
  13.     unordered_set<string>::iterator it;
  14.     int choice, item;
  15.     string s;
  16.     while (1)
  17.     {
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"Unordered Set 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 Set: "<<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 Set:  ";
  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. }

$ g++ unorderedset_stl.cpp
$ a.out
 
---------------------
Unordered Set 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: Neptune
 
---------------------
Unordered Set 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: Pluto
 
---------------------
Unordered Set 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 Set: 9
 
---------------------
Unordered Set 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 Set:   Neptune Uranus Mercury Pluto Venus Mars Jupiter Earth Saturn
 
---------------------
Unordered Set 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: Earth
 
---------------------
Unordered Set 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 Set: 8
 
---------------------
Unordered Set 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
Neptune is in bucket #16
Uranus is in bucket #9
Mercury is in bucket #2
Pluto is in bucket #15
Venus is in bucket #15
Mars is in bucket #5
Jupiter is in bucket #7
Saturn is in bucket #4
 
---------------------
Unordered Set 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
myset has 17 buckets.
 
---------------------
Unordered Set 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
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.