Hello everyone!
This time I am going to tell you about MAP -> a very very important stuff that saves lot of your time during a coding contest .
Just like stack , queue , map is also a container class of STL .
A map contains two values , one of them is the key and other is the value stored at that key . A particular value can be accessed or modified by using its key . A key of a value is unique i.e map has unique keys which can not be modified .
following example will clarify everything :
#include<iostream>
#include<string.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,int>employees; // here the first one(string) is the key and int is the value stored at
//at that key
employees.insert(pair<string,int>("vikram",1932)) ; //inserting values into map
employees.insert(pair<string,int>("Rahul" ,1231)) ;
employees.insert(pair<string,int>("arjun" ,1012)) ;
cout<<"map size : "<<employees.size()<<endl; // to get the size of map
// iterating thorugh a map
// In STL iterators provide a means for accessing data stored in container classes such a vector
// list , map etc .
// declaring an iterator -> class_name<template_parameters>::iterator name
map<string,int>::iterator it = employees.begin(); // referencing iterator to the beginning of map
for(it; it!= employees.end() ; it++ )
{
cout<< it->first<<" "<<it->second<<endl; // first gives you the key ,, second gives you value
}cout<<endl;
//iterating the map in reverse manner
map<string,int>::reverse_iterator bt = employees.rbegin();
for(bt ; bt!= employees.rend(); bt++)
{
cout<< bt->first<<" "<<bt->second<<endl;
}
/* to check whether a key is present in the map */
// suppose we want to check if a key named "arjun" is present in the map or not
// for this we use map.count(key_name) ; it returns value >0 if that key is already in map else 0
if( employees.count("arjun")>0){cout<<"this key is present "<<endl; }
/* accessing/modifying value stored at a particular key*/
// accessing :
cout<<"value stored at arjun : "<<employees.at("arjun")<<endl;
//modifying :
employees.at("arjun") = 500;
// inserting new key using [ ] :
employees ["tata"] = 123;
cout<<"value stored at tata : "<<employees.at("tata")<<endl;
}
output is :
map size : 3
Rahul 1231
arjun 1012
vikram 1932
vikram 1932
arjun 1012
Rahul 1231
this key is present
this key is present
value stored at arjun : 1012
value stored at tata : 123
This time I am going to tell you about MAP -> a very very important stuff that saves lot of your time during a coding contest .
Just like stack , queue , map is also a container class of STL .
A map contains two values , one of them is the key and other is the value stored at that key . A particular value can be accessed or modified by using its key . A key of a value is unique i.e map has unique keys which can not be modified .
following example will clarify everything :
#include<iostream>
#include<string.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,int>employees; // here the first one(string) is the key and int is the value stored at
//at that key
employees.insert(pair<string,int>("vikram",1932)) ; //inserting values into map
employees.insert(pair<string,int>("Rahul" ,1231)) ;
employees.insert(pair<string,int>("arjun" ,1012)) ;
cout<<"map size : "<<employees.size()<<endl; // to get the size of map
// iterating thorugh a map
// In STL iterators provide a means for accessing data stored in container classes such a vector
// list , map etc .
// declaring an iterator -> class_name<template_parameters>::iterator name
map<string,int>::iterator it = employees.begin(); // referencing iterator to the beginning of map
for(it; it!= employees.end() ; it++ )
{
cout<< it->first<<" "<<it->second<<endl; // first gives you the key ,, second gives you value
}cout<<endl;
//iterating the map in reverse manner
map<string,int>::reverse_iterator bt = employees.rbegin();
for(bt ; bt!= employees.rend(); bt++)
{
cout<< bt->first<<" "<<bt->second<<endl;
}
/* to check whether a key is present in the map */
// suppose we want to check if a key named "arjun" is present in the map or not
// for this we use map.count(key_name) ; it returns value >0 if that key is already in map else 0
if( employees.count("arjun")>0){cout<<"this key is present "<<endl; }
/* accessing/modifying value stored at a particular key*/
// accessing :
cout<<"value stored at arjun : "<<employees.at("arjun")<<endl;
//modifying :
employees.at("arjun") = 500;
// inserting new key using [ ] :
employees ["tata"] = 123;
cout<<"value stored at tata : "<<employees.at("tata")<<endl;
}
output is :
map size : 3
Rahul 1231
arjun 1012
vikram 1932
vikram 1932
arjun 1012
Rahul 1231
this key is present
this key is present
value stored at arjun : 1012
value stored at tata : 123
you can see that value stored in a map is already sorted . Time complexity of std:: map is of log(n)
I will be writing about tie complexity in another post .
If regarding map you have any doubt , visit this page this one
Here are some questions which can be solved using map . Please go through this link if you hate Ramsay Bolton and enjoyed the Battle of Bastards :)
Here are some questions which can be solved using map . Please go through this link if you hate Ramsay Bolton and enjoyed the Battle of Bastards :)
No comments:
Post a Comment