Here I want to clear the concepts of map in short.There are 4 associate containers (binary tree) : set ,multiset ,map and multimap .Applications of map are very important because, both multiset and maps are sorted binary trees, so inserting/finding 1 out of N entries takes O(log N).As we will focus

include map library :

#include<map>

Declaration:

map<data_type,data_type>  map_name ;

 

ex: map<char ,int> myMap;

char is key type and int is key value.

Insertion :

Various  ways :

  1. myMap.insert(pair <char,int> (‘a’ ,100));
  2. myMap.insert(make_pair(‘a’,100));
  3. map<char , int>::iterator it = myMap.begin(); myMap.insert(it ,pair<char,int> (‘a’ ,100));
  4. myMap[‘a’] =100;
  5. myMap.insert(map <char,int> ::value_type(‘a’,100));

Finding :

myMap.find(key_type);

size :

myMpa.size(); returns int .

These are the some imp. and simple things you need to know for using map.Now practice some problem using map.

Here is a example I found :

// std::map example
// opposite words

#include 
#include

#include 
using namespace std;

typedef std::map TStrStrMap;
typedef std::pair TStrStrPair;

int main(int argc, char *argv[])
{
    TStrStrMap tMap;

    tMap.insert(TStrStrPair("yes", "no"));
    tMap.insert(TStrStrPair("up", "down"));
    tMap.insert(TStrStrPair("left", "right"));
    tMap.insert(TStrStrPair("good", "bad"));

    std::string s;
    std::cout << "Enter word: " <> s;

    
    std::string strValue = tMap[s];
    if(strValue!="")
    {
        // Show value
        std::cout << "Opposite: " << strValue <second;
            if( strValue  == strKey)
            {
                // Return key
                std::cout << "Opposite: " <
first << std::endl;
                bFound = true;
            }
        }
        // If not found opposite word
        if(!bFound)
        {
            std::cout << "Word not in map." << std::endl;
        }
    }
    return 0;
}


Hope you learnt something , stay tune for more examples and interesting facts about c++.
#keepCoding