-
Notifications
You must be signed in to change notification settings - Fork 0
/
stlmap.cpp
50 lines (50 loc) · 851 Bytes
/
stlmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<char,string> m;
map<char,string>::iterator it;
int n,i;
char a;
string s;
cout<<"Enter no. of map elements \n";
cin>>n;
for( i=1;i<=n;i++)
//To insert elements
{
cout<<" KEY-VALUE :";
cin>>a;
cin>>s;
m.insert(pair<char,string>(a,s)); //OR m[a]=s;
}
//To Delete the element
cout<<"Enter the Value\n";
cin>>s;
it=m.begin();
for( ;it!=m.end();it++)
{
if(it->second==s)
{
m.erase(it);
break;}
}
if(it == m.end())
cout<<"Element not found \n";
else
cout<<"Item found \n";
//Finding element using key
cout<<"Enter the key \n";
cin>>a;
it=m.find(a);
if(it != m.end())
cout<<it->second<<endl<<endl;
else
cout<<"Element not found \n\n";
cout<<m.size()<<endl<<endl;
//To display contents of map.
for(it=m.begin();it!=m.end();it++)
{
cout<<"Key="<<it->first<<" ITEM="<<it->second<<endl;
}
}