Map in C++ made easy

Hi everyone, lets move on to C++ for a while. This tutorial will help you in learning Map in C++. I have written in the simplest possible way. Before coming to C++, please refer my previous post about Map and Set in Java made easy. We won't be covering any implementation of Map in this post.

MAP

Similar to the maps in Java, map in C++ is an associative container that stores key and a mapped value. An associative containers refer to the group of class templates in the standard library of C++ programming language that implement ordered arrays.

In map in C++, all the elements are ordered. The key values will be unique.

Use

Maps are mainly used, whenever we want to store particular occurrence of a variable. For e.g.: - Program to count the occurrence of all words in a file.

Defining a Map

Map in C++ is defined as follows: -

map<string,int> map1;

Instead of string and int, we can use other datatype as well.We should include the header file - #include<map>

Adding key and values to Map

Adding an item to Map in different from Java. It is as follows: - map1[key] = value;
Here, 'key' is the key and 'value' is the value.

Retrieving a value from Map

int num = map1[key];
The variable num gets the value of the key 'key'.

Checking whether a key is present in the Map

bool find = map1.count(item);
If map1 contains the key 'item', then the variable find will be true.

Iterating a Map

A map is traversed using an iterator. It is as follows: -

map<string, int> :: iterator it;
for(it = map1.begin(); it!= map1.end(); ++it)
{
cout<<it->first<<setw(10)<<it-> second;
}

Here it->first refers to the key and it->second refers to value.

Sample Program in C++ to demonstrate the use of Map

Program to print the frequent element in an array.


#include<iostream>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include <string>

using namespace std;
int frequentElement(vector<int> temp);

int main()
{
    vector<int> temp;
    int array[25]={1,2,3,4,1,2,35,6,7,3,7,8,8,9,10,12,11,1,1,4,5,3,2,1,6};
    
    for(int i=0;i<25;i++)
    {
        temp.push_back(array[i]);
    }
    int frequent = frequentElement(temp);
    cout<<"The frequent item in the array = "<<frequent<<endl;
    return 0;
}

int frequentElement(vector<int> temp)
{
    map<int,int> countMap;
    for(int i=0;i<temp.size();i++)
    {
        if(countMap.count(temp[i]))
        {
            int getnum=countMap[temp[i]];
            getnum=getnum+1;
            countMap[temp[i]]=getnum;
        }
        else
        {
            countMap[temp[i]]=1;
        }
           
    }
    
    map <int, int>::iterator it;
    int maxvalue=0;
    int returnvalue=0;
    for (it = countMap.begin(); it != countMap.end(); ++it)
    {   
        if(it->second >maxvalue)
        {
            maxvalue=it->second; 
            returnvalue=it->first;
            
        }
        
    }  
    return returnvalue;
    
}


Please comment below if you have any doubts. All comments will be responded within a days time. Thank you for reading.

Similar Posts

1) Map and sets in Java made easy - sujithforu.blogspot.com/2012/11/map-and-set-in-java-in-made-easy.html

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++