-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cpp
39 lines (39 loc) · 1.01 KB
/
Map.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
#include<iostream>
#include<map>
#include<string>
#include<stdexcept>
using namespace std;
int main()
{
string name;
int number;
map<string, int> phone;;
cout<<"\nEnter three sets of Names & Phone Numbers:\n";
for(int i=0; i<3; i++)
{
cin>>name;
cin>>number;
phone[name]=number;
}
cout<<"\nInserting Name: Aleyna & Number: 4949";
phone["Aleyna"]=4949; //Insert another element
cout<<"\nInserting Name: Bose & Number: 5511";
phone.insert(pair<string, int> ("Bose", 5511)); //Since each element of a map is a pair
cout<<"\nSize of Map: "<<phone.size();
cout<<"\n\nTherefore, Telephone Records are: ";
map<string, int> ::iterator p;
for(p=phone.begin();p!=phone.end();p++)
cout<<"\n"<<(*p).first<<" : "<<(*p).second;
cout<<"\n\nEnter Name whose Number is to be found: ";
cin>>name;
try
{
number=phone.at(name);
cout<<"\nNumber: "<<number;
}
catch(std::out_of_range &e)
{
cout<<"\nRecord for this name does not exist in the phonebook. ";
}
return 0;
}