Friday 24 November 2017

C++ Program to Implement Unordered set in STL


Code:

#include     iostream
#include     string
#include     cstdlib
#include     unordered_set
using namespace std;
int main()
{
    unordered_set myset = {"Mercury","Venus","Earth", "Mars","Jupiter","Saturn","Uranus"};
    unordered_set::const_iterator got;
    unordered_set::iterator it;
    int choice, item;
    string s;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Unordered Set Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element"<
        cout<<"2.Delete Element"<
        cout<<"3.Find Element"<
        cout<<"4.Count Elements"<
        cout<<"5.Size of the Unordered Set"<
        cout<<"6.Count number of Buckets"<
        cout<<"7.Buckets"<
        cout<<"8.Display Unordered Set"<
        cout<<"9.Exit"<
        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"<
            else
                cout<<"Element not found in myset"<
            break;
        case 4:
            cout<<"Enter string to be counted: ";
            cin>>s;
            cout<<"There are "<
            break;
        case 5:
            cout<<"Size of the Unordered Set: "<
            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 Set:  ";
            for (it = myset.begin(); it != myset.end(); ++it)
                cout << " " << *it;
            cout<
            break;
case 9:
            exit(1);
    break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

---------------------
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


More C++ Programs:






















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...