-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
51 lines (42 loc) · 1.15 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
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int main()
{
// declaring map
// of char and int
map<string, int > mp;
// declaring iterators
map<string, int>::iterator it ;
map<string, int>::iterator it1 ;
// inserting values
mp["zero"]=0;
mp["one"]=1;
mp["two"]=2;
mp["three"]=3;
mp["four"]=4;
// using find() to search for 'b'
// key found
// "it" has address of key value pair.
it = mp.find("one");
if(it == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair present : "
<< it->first << " - " << it->second ;
cout << endl ;
// using find() to search for 'm'
// key not found
// "it1" has address of end of map.
it1 = mp.find("anything");
if(it1 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair present : "
<< it1->first << "->" << it1->second ;
return 0;
}