C++ : Map Tutorial Part 1: Usage Detail with examples

In the previous article, we have discussed about C++ : How to Sort a List of objects with custom Comparator or lambda function. Let us learn About Map Tutorial Part 1 in C++ Program.

Map Tutorial Part 1

In this article we will discuss see how and why to use std::map in C++.

Std::key is a sorted associative container that contains key-value pairs with unique keys.

Properties of std::key :

  • Std::map can be used as associative arrays.
  • In std::key there will be only one value attached with every key.
  • It might be implemented using balanced binary trees.
  • It stores only unique keys and that too in sorted order based on its assigned sorting criteria.
  • As keys are in sorted order therefore searching elements is fast i.e. it takes logarithmic time.

Let’s see an example:

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords.insert(std::make_pair("Gold", 1));
    mapOfWords.insert(std::make_pair("Silver", 2));
    mapOfWords["Diamond"] = 3;
    mapOfWords["Gold"] = 4;
    std::map<std::string, int>::iterator it = mapOfWords.begin();
    while(it != mapOfWords.end())
    {
        std::cout<<it->first<<" :: "<<it->second<<std::endl;
        it++;
    }
    if(mapOfWords.insert(std::make_pair("Gold", 1)).second == false)
    {
        std::cout<<"'Gold' already existed"<<std::endl;
    }
    if(mapOfWords.find("Diamond") != mapOfWords.end())
        std::cout<<"'Diamond' found"<<std::endl;
    if(mapOfWords.find("Platinum") == mapOfWords.end())
        std::cout<<"'Platinum' not found"<<std::endl;
    return 0;
}

Output :

Diamond :: 3

Gold :: 4

Silver :: 2

'Gold' already existed

'Diamond' found

'Platinum' not found

Creating std::map objects :

std::map<std::string, int> mapOfWords;
  • No external sorting criteria for key(std::string) is specified here.
  • So, it will use default key sorting criteria i.e operator <
  • As result all elements will be arranged inside std::map in alphabetical sorted order of keys.

Inserting data in std::map :

Inserting data using insert member function

Let’s see an example:

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords.insert(std::make_pair("Gold", 1));
    mapOfWords.insert(std::make_pair("Silver", 2));
    std::map<std::string, int>::iterator it = mapOfWords.begin();
    while(it != mapOfWords.end())
    {
        std::cout<<it->first<<" :: "<<it->second<<std::endl;
        it++;
    }
    return 0;
}
Output :

Gold :: 1

Silver :: 2

Inserting data using operator[] :

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords["Diamond"] = 1;
    std::map<std::string, int>::iterator it = mapOfWords.begin();
    while(it != mapOfWords.end())
    {
        std::cout<<it->first<<" :: "<<it->second<<std::endl;
        it++;
    }
    return 0;
}
Output:

Diamond :: 1

Iterating through all std::map elements :

As map items are contained like std::pair<std::string, int> so through iterator it-first will access key and it-second will access value.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords["Diamond"] = 1;
    mapOfWords["Platinum"] = 2;
    std::map<std::string, int>::iterator it = mapOfWords.begin();
    while(it != mapOfWords.end())
    {
        std::cout<<it->first<<" => "<<it->second<<std::endl;
        it++;
    }
    return 0;
}



Output:

Diamond => 1
Platinum => 2

Searching element in std::map by key :

find() member function will be used to search an element in the map. And specified key is searched in the whole map.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords.insert(std::make_pair("Gold", 1));
    mapOfWords.insert(std::make_pair("Silver", 2));
    mapOfWords["Diamond"] = 3;
    mapOfWords["Gold"] = 4;
    if(mapOfWords.find("Diamond") != mapOfWords.end())
        std::cout<<"'Diamond' found"<<std::endl;
    if(mapOfWords.find("Platinum") == mapOfWords.end())
        std::cout<<"'Platinum' not found"<<std::endl;
    return 0;
}

Output:

'Diamond' found
'Platinum' not found

Searching element in std::map by value :

All the elements needs to be iterated to check for the passed value and return.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
std::map<std::string, int>::iterator serachByValue(std::map<std::string, int> & mapOfWords, int val)
{
    std::map<std::string, int>::iterator it = mapOfWords.begin();
    while(it != mapOfWords.end())
    {
        if(it->second == val)
        return it;
        it++;
    }
}
int main()
{
    std::map<std::string, int> mapOfWords;
    mapOfWords.insert(std::make_pair("Gold", 1));
    mapOfWords.insert(std::make_pair("Silver", 2));
    mapOfWords["Diamond"] = 3;
    std::map<std::string, int>::iterator it = serachByValue(mapOfWords, 3);
    if(it != mapOfWords.end())
        std::cout<<it->first<<" :: "<<it->second<<std::endl;
return 0;
}

Output :

Diamond :: 3

Deleting data from std::map :

To delete the element in std::map we have erase() member function.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
int main()
{
    std::map<std::string, int> mapOfWords;
// elements are inserted
    mapOfWords.insert(std::make_pair("Gold", 1));
    mapOfWords.insert(std::make_pair("Silver", 2));
    mapOfWords["Diamond"] = 3;
    std::map<std::string, int>::iterator it = mapOfWords.find("Gold");

// erased element Silver
    mapOfWords.erase("Silver");
// searching silver after erasing, so element can not be found    
    if(mapOfWords.find("Silver") != mapOfWords.end())
        std::cout<<"word 'silver' found"<<std::endl;
    else
        std::cout<<"Elements not found"<<std::endl;
return 0;
}
Output:

Elements not found

Other elements which are present in std::map can be checked liked this. As like we checked the element “Silver”here.