Map implementation in STL

Map

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;

Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

Map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).

Maps are typically implemented as binary search trees.

C++ Program for implementation of Maps in STL:

#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    map<char,int> mp;
    map<char, int>::iterator it;
    int choice, item;
    char s;
    while (1)
    {
        cout<<"n---------------------"<<endl;
        cout<<"Map Implementation in Stl"<<endl;
        cout<<"n---------------------"<<endl;
        cout<<"1.Insert Element into the Map"<<endl;
        cout<<"2.Delete Element of the Map"<<endl;
    cout<<"3.Size of the Map"<<endl;
        cout<<"4.Find Element at a key in Map"<<endl;
        cout<<"5.Dislplay by Iterator"<<endl;
        cout<<"6.Count Elements at a specific key"<<endl;
        cout<<"7.Exit"<<endl;
        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<char,int>(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<<mp.size()<<endl;
            break;
        case 4:
        cout<<"Enter the key at which value to be found: ";
            cin>>s;
            if (mp.count(s) != 0)
                cout<<mp.find(s)->second<<endl;
            else
                cout<<"No Element Found"<<endl;
            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<<mp.count(s)<<endl;
            break;
    case 7:
            exit(1);
        break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

Sample 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


------------------

Leave a Comment