MAP Function in C++ STL
MAP Function in C++ STL
The map function attributes values to certain keys. As can be seen above, the circle is matched with a circle, and so on. When the 'int' data type is attributed, the keys are automatically arranged in ascending order.
The map can be manipulated in certain ways so that the value is always mapped to the key. So if you remove the key, the value is also removed. You move the key, and the value is also moved.
Here is the syntax to create and display user-given inputs to a map.
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int,int> example;
int n,t1,t2;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t1>>t2;
example.insert(pair<int,int>(t1,t2));
}
cout<<"----------------------"<<endl;
map<int,int>::iterator i;
for(i=example.begin();i!=example.end();i++)
{
cout<<i->first<<" "<<i->second<<endl;
}
The important STL function here to know are
- map<int,int> name_of_map;
- name_of_map.insert(pair<int,int>(input_1,input_2));
- name_of_map.begin();
- name_of_map.end();
- map<int,int>::iterator i;
- i->first
- i->second
map<int,int> even;
map<int,int> odd;
for(i=example.begin();i!=example.end();i++)
{
if(i->first%2==0)
even.insert(pair<int,int>(i->first,i->second));
else
odd.insert(pair<int,int>(i->first,i->second));
}
cout<<"----------------------"<<endl;
map<int,int>::iterator j;
for(j=even.begin();j!=even.end();j++)
cout<<j->first<<" "<<j->second<<endl;
cout<<"----------------------"<<endl;
for(j=odd.begin();j!=odd.end();j++)
cout<<j->first<<" "<<j->second<<endl;
}
This part of the code shows how you can manipulate the map to get desired results.
Here is a sample output.
Comments
Post a Comment