STL Map all operations example - map all algorithms

What is a map?
A map is an associative container which stores elements in key-value pair.
By default elements are stored in sorted fashion in ascending order of key. Binary search tree is internal data structure to implement maps.
How to insert in a map?
i) using 'insert" algorithm or
ii)using [key]=value style.
What is the difference between above two ways of insertion in maps?
What happens when a value is inserted in a map    ?
It depends on what algorithm has been used to insert.
i)If "insert.(pair<keyType,valuType>(key,value))" is used then value is simply gets ignored if that key already exists.
ii)If [key]=value is used then new value gets replaced if that key already exists.
iii)If at(key)=value is used then also new value is update to key.
How to delete using key?
By "find" and "erase" algorithm.
C++ MAP STL example code:
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>
int main()
{
map<int,string> m;//actually <string, int> example is more used
m.insert(pair<int,string>(1,"Ram"));
m.insert(pair<int,string>(4,"Ram"));
m.insert(pair<int,string>(8,"Ram"));
m.insert(pair<int,string>(3,"Ram"));
m.insert(pair<int,string>(5,"Ram"));
m.insert(pair<int,string>(500,"Ram"));
m.insert(pair<int,string>(50,"Ram"));
m.insert(pair<int,string>(100,"Ram"));
m[1000]="Supreme";


cout<<"map is:"<<endl;
map<int,string>::iterator itr;
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;//sorted keys

cout<<"Erase all with key 4"<<endl;
int del=m.erase(4);
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;

cout<<"erase all less than key 8"<<endl;
m.erase(m.begin(),m.find(8));
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;

//find(const g) – Returns an iterator to the element with key value ‘g’ in the map if found, else returns the iterator to end
cout<<"Finding key 100 and deleting it."<<endl;
map<int,string>::iterator itr2=m.begin();
itr2=m.find(100);
if(itr2!=m.end())
{
cout<<"100 found"<<endl;
m.erase(itr2);
//m.at(100) = "NewValue";
}
else
{
cout<<"100 not found"<<endl;
}
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;

cout<<"Deleting key 50"<<endl;
m.erase(50);
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;

cout<<"Inserting using at(key)"<<endl;
m.at(8) = "Raheem";
m.at(500) = "Jesus";
//m.at(100) = "God";//out of range when tries to insert at a key which does not exists
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;

cout<<"Find(key)->second display."<<endl;
cout << "8 => " << m.find(8)->second<<endl;
cout << "500 => " << m.find(500)->second<<endl;
//cout << "500 => " << m.find(100)->second<<endl;//empty

cout<<endl;
return 0;
}
Example 2:
What is the difference between two different ways of inserting into a map?
What will happen if duplicate values are inserted in a map?
i) inserting using algorthm"insert" : Does nothing. Key holds the old value.
ii) inserting using [] : update the key with new value.
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>
int main()
{
cout<<""<<endl;
map<string,int> m;
//In map values are inserted in asending order of keys.
m.insert(pair<string,int> ("Manoj",5));
m.insert(make_pair("Sony",2));
m.insert(make_pair("Aditi",3));
//insert resturns false if same key is inserted again.
//Nothing happens if we insert duplicatge key.
m.insert(make_pair("Aditi",4));//Not inserted as key Aditi already exists.
m.insert(make_pair("Aditi",5));//Not inserted as key Aditi already exists.
//Different keys can have the same value as another key's value.
m.insert(make_pair("Biswa",3));//Biswa inserted after Aditi.

map<string,int>::iterator itr=m.begin();
while(itr!=m.end())
{
cout<<itr->first<<" "<<itr->second<<endl;
itr++;
}
/*
Aditi 3
Biswa 3
Manoj 1
Sony 2
*/
cout<<"Another map to show second way of inserting elements:\n";
map<string,int> m2;
m2.insert(make_pair("AAA",1));
m2.at("AAA")=300;//now value of key AAA is also gets updated
m2["BBB"]=2;//now value of key BBB is 2
m2["BBB"]=3;//now value of key BBB is 3
m2["BBB"]=4000;//now value of key BBB is 4000
map<string,int>::iterator itr2=m2.begin();
while(itr2!=m2.end())
{
cout<<itr2->first<<" "<<itr2->second<<endl;
itr2++;
}
/*
AAA300
BBB 4000
*/
return 0
 }
Multi Map example to hold duplicate keys:
#include<iostream>
using namespace std;
#include<map>
#include<algorithm>
int main()
{
cout<<""<<endl;
multimap<string,int> m;
//In map values are inserted in asending order of keys.
m.insert(pair<string,int> ("Manoj",5));
m.insert(make_pair("Sony",2));
m.insert(make_pair("Aditi",3));
m.insert(make_pair("Aditi",4));//Another Aditi with new value 4
m.insert(make_pair("Aditi",5));//Another Aditi with new value 5
m.insert(make_pair("Biswa",3));//Biswa inserted after Aditi.

multimap<string,int>::iterator itr=m.begin();
while(itr!=m.end())
{
cout<<itr->first<<" "<<itr->second<<endl;
itr++;
}
/*
Aditi 3
Aditi 4
Aditi 5
Biswa 3
Manoj 5
Sony 2
*/
cout<<"---map example to find a key"<<endl;
cout<<"---find returns iterator to the found keys"<<endl;
cout<<"---find returns false so garbage value"<<endl;
itr=m.begin();
cout<<"m.find(Aditi)="<<m.find("Aditi")->second<<endl;//3//first match only.
cout<<"m.find(Manoj)="<<m.find("Manoj")->second<<endl;//5
cout<<"m.find(Sahu)="<<m.find("Sahu")->second<<endl;//garbage

itr=m.begin();
if(m.find("Aditiii")!=m.end())
cout<<itr->first<<" found and value is "<<itr->second<<endl;
else
cout<<"Not found"<<endl;

return 0;
}
/*
Aditi 3
Aditi 4
Aditi 5
Biswa 3
Manoj 5
Sony 2
---map example to find a key
---find returns iterator to the found keys
---find returns false so garbage value
m.find(Aditi)=3
m.find(Manoj)=5
m.find(Sahu)=223444160
Not found
*/ 
In multi map "find" return the value of first key only.
How to find all the values associated with duplicate key?
Using the algorithm equal_range("key").
Count can be taken by algorithm "distance" 

No comments:

Post a Comment