Examples to show the use of Custom Map

The following programmes demonstrate the execution for the Map class by creating its functionalities.

Example 1: Programs to demonstrate the use of insert(), accessing any key and update() methods:

C++




#include "map.h"
#include <iostream>
using namespace std;
 
int main()
{
    Map map;
 
    // 1st way of insertion
    map[132] = 3;
    map[34] = 5;
    map[42] = -97;
    map[22] = 10;
    map[12] = 42;
 
    // 2nd way of insertion
    map.insert(-2,44);
    map.insert(0,90);
 
    // accessing elements
    cout<<"Value at key 42 before updating = "
        <<map[42]<<endl;
    cout<<"Value at key -2 before updating = "
        <<map[-2]<<endl;
    cout<<"Value at key 12 before updating = "
        <<map[12]<<endl;
 
    // Updating value at key 42
    map[42] = -32;
 
    // Updating value at key -2
    map.insert(-2,8);
 
    // Updating value at key 12
    map.update(12,444);
 
    // accessing elements
    cout<<"Value at key 42 after updating = "
        <<map[42]<<endl;
    cout<<"Value at key -2 after updating = "
        <<map[-2]<<endl;
    cout<<"Value at key 12 after updating = "
        <<map[12]<<endl;
    cout<<"Value at key 0 = "<<map[0]<<endl;   
    return 0;
}


Output:

 

Example 2: Programme to demonstrate the use of erase(), clear() and iterate() methods:

C++




#include "map.h"
#include <iostream>
using namespace std;
 
int main()
{
    Map map;
    map[132] = 3;
    map[34] = 5;
    map[42] = -97;
    map[22] = 10;
    map[12] = 42;
 
    // Iterating the Map elements before erasing 22
    map.iterate();
    map.erase(22);
 
    // Iterating the Map elements after erasing 22
    map.iterate();
     
    // Deleting the whole map
    map.clear();
 
    // Now since there are zero elements
    // in the Map the output is blank
    cout<<"\nElements in Map after clear operation: ";
    map.iterate();
    return 0;
}


Output: 

 

Example 3: Programme to demonstrate the use of find(), count(), empty() and size() methods:

C++




#include "map.h"
#include <iostream>
using namespace std;
 
int main()
{
    Map map;
    map[132] = 3;
    map[34] = 5;
    map[42] = -97;
    cout<<"Value at 132 before updating = "
        <<map[132]<<endl;
 
    // Find method returns pointer to element
    // whose key matches given key
    Map *it = map.find(132);
 
    // Updating the value at key 132
    it->second = 98;
 
    cout<<"Value at 132 after updating = "
        <<map[132]<<endl;
 
    // Count of an element which is not present
    // in the map is 0
    cout<<"Count of 77 = "<<map.count(77)<<endl;
 
    // Count of an element which is present
    // in the map is 1
    cout<<"Count of 34 = "<<map.count(34)<<endl;
 
    // Size of map/number of elements in map
    cout<<"Map size = "<<map.size()<<endl;
 
    // Map is not empty therefore returned 0
    cout<<"Is map empty: "<<map.empty()<<endl;
 
    // Clearing the map
    map.clear();
 
    // Map is empty therefore return 1
    cout<<"Is map empty: "<<map.empty()<<endl;
    return 0;
}


Output:

 



Build a custom Map using Header file in C++

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. Maps are implemented by self-balancing search trees. In C++ STL it uses Red-Black Tree.

Here we are going to implement a custom Map class which has an integer value as the key and the value stored corresponding to any key is also of integer type.  

We will implement it using the AVL tree. To implement the map, we will first create a header file which will incorporate the functionalities of a map class. Below is the basic structure of the Map class:

Similar Reads

Structure of Map class:

The structure of the AVL tree depends upon the structure of the node:...

Functions/Operations To Be Implemented Using Custom Map

...

Creation of Custom Map Header

We will cover the following functionalities and methods in our map:...

How to Execute the built Custom Map

...

Examples to show the use of Custom Map

...

Contact Us