Map in c++ - Standard Template Library (STL)
Map in c++ are the containers that stores elements in the form of keys and values in a mapped format.
You can use map for solving various problems,like if you want to calculate any element's frequency.
Syntax for map in c++:
map<type,type> name_of_map;
For ex:
map<int,int>m1;
where,key type is integer
value type is integer
name of map is 'm1'
Header file
Header file of map in c++ is #include<map>.
Program for counting frequency of elements:
#include<iostream>
#include<map>
using namespace std;
int main()
{
int a[]={1,2,4,19,10,23,1,1,2,4,23,4,4,19};
map<int,int>m1;
for(int i=0;i<14;i++)
{
m1[a[i]]++; //counting elements
}
cout<<"Frequency of 1 is :"<<m1[1]<<endl;
cout<<"Frequency of 2 is :"<<m1[2]<<endl;
cout<<"Frequency of 4 is :"<<m1[4]<<endl;
cout<<"Frequency of 19 is :"<<m1[19]<<endl;
cout<<"Frequency of 10 is :"<<m1[10]<<endl;
cout<<"Frequency of 23 is :"<<m1[23]<<endl;
}
Comments
Post a Comment