How to create insert and read map in map using std::map

Let check how we can code for following image where there are 3 Maps placed inside one another. As we know Map as two elements 1.Key and 2.Data (mapped to key) here data is nothing but an another map.

typedef std::map<string,vector<string>> InnerMostMap;
typedef std::map<string,vector<string>>::iterator InnerMostMapItr;

typedef std::map<string,InnerMostMap> MidMap;
typedef std::map<string,InnerMostMap>:: iterator MidMapItr;
or 
std::map<string,std::map<string,vector<string>>MidMap;

typedef std::map<string,MidMap> OutMap;
typedef std::map<string,MidMap>::iterator OutMapItr;
or
std::map<string,std::map<string,std::map<string,vector<string>>>OutMap


Nested Map, [Map inside Map]


Strategy to follow:
  • If KEY is to be added for the first time in map then one can use directly use map::insert()
  • If KEY is already exist and then you want to add/modify the data against that key then
    • use map::find() to know at which place KEY exist in map
    • then modify/update the data.
  • It is very simple to use auto datatype rather iterator.
  • Best practice is to use try and catch.
Let check following code to understand, COPY, PASTE on any online compiler & RUN

NOTE:- First check output of below code then come back to code again and focus of comments.

#include<iostream>

#include<map>
#include<vector>
using namespace std;

typedef std::map<string,vector<string>> InnerMostMap;
typedef std::map<string,vector<string>>::iterator InnerMostMapItr;

typedef std::map<string,InnerMostMap> MidMap;
typedef std::map<string,InnerMostMap>:: iterator MidMapItr;

typedef std::map<string,MidMap> OutMap;
typedef std::map<string,MidMap>::iterator OutMapItr;

//g_completeMap is a a
ctual map Object
//that we are going to fill and read

OutMap g_completeMap;

//Function to iterate complete map 
//and display values. i.e. read values from map

void display_map()
{
    for(auto l_outMapRowObj:g_completeMap )
    {
        cout<<"OutMap Key:: "<<l_outMapRowObj.first<<endl;
        for(auto l_midMapRowObj:l_outMapRowObj.second )
        {
            cout<<"\t MidMap Key::"<<l_midMapRowObj.first<<endl;
            for(auto l_innrMapRowObj:l_midMapRowObj.second )
            {
                cout<<"\t\t InnerMap Key::"<<l_innrMapRowObj.first<<endl;
                for(auto l_vectorElement : l_innrMapRowObj.second )
                {
                    cout<<"\t\t\t Vector Value::"<<l_vectorElement<<endl;
                }        
            }
        }
    }
}
int main()
{
    try
    {
        //First Entry in Inner Most map
        std::vector<string> l_vector ;
        l_vector.push_back("First_Entry_Vector");
        InnerMostMap l_innerMap;
        l_innerMap.insert(std::pair<string,vector<string>>("first_key_inner",l_vector));

        //First Entry in MidMap
        MidMap l_midMap;
        l_midMap.insert(std::pair<string,InnerMostMap>("first_key_mid",l_innerMap));

        //First entry in Actual OutMap
        g_completeMap.insert(std::pair<string,MidMap>("first_key_outer",l_midMap));

        //display Value- Check what is inserted in map.
        display_map();
        
//Check Output Now
        //**********************************************************//
        //Enter following New-Value in map on Existing KEY
        //first_key_outer::first_key_mid::first_key_inner::[Second_Entry_Vector]
        // Means only entry to be added in already existing Vector
        //**********************************************************//

        OutMapItr l_outMapItr;
        l_outMapItr=g_completeMap.find("first_key_outer");
        if(l_outMapItr == g_completeMap.end())
        {
            cout<<"first_key_outer not foud in MAP" <<endl;
            //Write code here if we want to add new entry in
            //outmap with new Key

        }
        else
        {
            //now we have reached to first_key_outer
            //now go to second of first_key_outer
            //i.e. (Value of first_key_outer)
            //then in value reach to first_key_mid


            //Create Mid key iterator
            MidMapItr l_midKeyItr;

            //with l_outMapItr->second reaches to MidMap
            //with .find() looks for first_key_mid in MidMap

            l_midKeyItr=(l_outMapItr->second).find("first_key_mid");

    
        if(l_midKeyItr == (l_outMapItr->second).end())
            {
                //Key first_key_mid is not found
            }
            else
            {
                //first_key_mid is found
                //now go to second of first_key_mid
                //i.e. (Value of first_key_mid)
                //then in that Value (second)find for first_key_inner
                
                //Create Inner Map iterator

                InnerMostMapItr l_innerMapItr;

                //with l_midKeyItr->second reaches to MidMap
                //with .find() looks for first_key_inner in MidMap

                l_innerMapItr=(l_midKeyItr->second).find("first_key_inner");

                if(l_innerMapItr == (l_midKeyItr->second).end())
                {
                    //first_key_inner not found
                }
                else
                {
                    //now go to first_key_inner value i.e Vector
                    //l_innerMapItr->second i.e. reaches to Vector
                    //Now push_back to put "Second_Entry_Vector"


                    (l_innerMapItr->second).push_back("Second_Entry_Vector");
                }
            }
        }
        cout<<"*** Second_Entry_Vector - inserted ***"<<endl;
        display_map();
        //Check Output Now
        //****************************************************//
        //Enter following Value in map
        // first_key_outer::first_key_mid::second_key_inner::[first_Entry_Vector]
        // Means only entry to be added in already existing Vector
        //*****************************************************//
        l_outMapItr=g_completeMap.find("first_key_outer");
        if(l_outMapItr == g_completeMap.end())
        {
            cout<<"first_key_outer not foud in MAP" <<endl;
            //Write code here if we want to add new entry in
            //outmap with new Key

        }
        else
        {
            //now we have reached to first_key_outer
            //now go to second of first_key_outer
            //i.e.(Value of first_key_outer)
            //then in value/data reach to first_key_mid

            //Create Mid key iterator

            MidMapItr l_midKeyItr;

            //with l_outMapItr->second reaches to MidMap
            //with .find() looks for first_key_mid in MidMap

            l_midKeyItr=(l_outMapItr->second).find("first_key_mid");
            if(l_midKeyItr == (l_outMapItr->second).end())
            {
                //Key first_key_mid is not found
            }
            else
            {
                //first_key_mid is found
                //now go to second of first_key_mid
                //i.e. (Value of first_key_mid)
                //then in that Value/Data (second)find for first_key_inner
                //Create Inner Map iterator
                InnerMostMapItr l_innerMapItr;

                //with l_midKeyItr->second reaches to MidMap
                //with .find() looks for first_key_inner in MidMap

                l_innerMapItr=(l_midKeyItr->second).find("second_key_inner");

                if(l_innerMapItr == (l_midKeyItr->second).end())
                {
                    //************************************/
                    //
second_key_inner not found 
                    //Create Vector to put at second_key_inner
                                               //*************************************/
                    std::vector<string> l_vector ;
                    l_vector.push_back("First_Entry_Vector");
                    (l_midKeyItr->second).insert(std::pair<string,vector<string>>("second_key_inner",l_vector));;
                }
                else
                {
                    //now go to first_key_inner value i.e Vector
                    //l_innerMapItr->second i.e. reaches to Vector
                    //Now push_back to put "Second_Entry_Vector"

                    (l_innerMapItr->second).push_back("Second_Entry_Vector");
                }
            }        
        }
               cout<<"*** second_key_inner - inserted ***"<<endl;
        //Check Output Now
        display_map();
    }
    catch(...)
    {
        cout<<"Exception "<<endl;
    }
}

 

 COMPILE:

g++ map.cpp –o map

 

 OUTPUT:

OutMap Key:: first_key_outer

         MidMap Key::first_key_mid

                 InnerMap Key::first_key_inner

                         Vector Value::First_Entry_Vector

*** Second_Entry_Vector - inserted ***

OutMap Key:: first_key_outer

         MidMap Key::first_key_mid

                 InnerMap Key::first_key_inner

                         Vector Value::First_Entry_Vector

                         Vector Value::Second_Entry_Vector

*** second_key_inner - inserted ***

OutMap Key:: first_key_outer

         MidMap Key::first_key_mid

                 InnerMap Key::first_key_inner

                         Vector Value::First_Entry_Vector

                         Vector Value::Second_Entry_Vector

                 InnerMap Key::second_key_inner

                         Vector Value::First_Entry_Vector 

 


Your Comments /Suggestions & Questions are always welcome. 

We would like to help you with best of our knowledge.

So feel free to put Questions

No comments:

Post a Comment