Friday 24 November 2017

C++ Program to Implement Map in STL


Code:

#include    iostream
#include    map
#include    string
#include    cstdlib
using namespace std;
int main()
{
    map mp;
    map::iterator it;
    int choice, item;
    char s;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Map Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element into the Map"<
        cout<<"2.Delete Element of the Map"<
cout<<"3.Size of the Map"<
        cout<<"4.Find Element at a key in Map"<
        cout<<"5.Dislplay by Iterator"<
        cout<<"6.Count Elements at a specific key"<
        cout<<"7.Exit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            cout<<"Enter the key: ";
            cin>>s;
            mp.insert(pair(s  ,item));
            break;
        case 2:
            cout<<"Enter the mapped string to be deleted: ";
            cin>>s;
            mp.erase(s);
            break;
        case 3:
    cout<<"Size of Map: ";
    cout<
            break;
        case 4:
    cout<<"Enter the key at which value to be found: ";
            cin>>s;
            if (mp.count(s) != 0)
                cout<second<
            else
                cout<<"No Element Found"<
            break;
        case 5:
    cout<<"Displaying Map by Iterator: ";
            for (it = mp.begin(); it != mp.end(); it++)
            {
                cout << (*it).first << ": " << (*it).second << endl;
            }
            break;
        case 6:
            cout<<"Enter the key at which number of values to be counted: ";
            cin>>s;
            cout<
            break;
case 7:
            exit(1);
    break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 1
Enter the key: a

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2
Enter the key: b

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 3
Enter the key: c

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
Enter the key: d

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 5
Enter the key: e

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 3
Size of Map: 5

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 4
Enter the key at which value to be found: a
1

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
c: 3
d: 4
e: 5

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 2
Enter the mapped string to be deleted: c

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
d: 4
e: 5

---------------------
Map Implementation in Stl

---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 7


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