Friday, 24 November 2017

C++ Program to Implement Unordered_multiset in STL


Code:

#include     iostream
#include     string
#include     cstdlib
#include     unordered_set
using namespace std;
int main()
{
    unordered_multiset myset = {"red","green","blue"};
    unordered_multiset::const_iterator got;
    unordered_multiset::iterator it;
    int choice, item;
    string s;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Unordered MultiSet 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 MultiSet: "<
            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<
            break;
case 9:
            exit(1);
    break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

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



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