-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmap.cpp
83 lines (63 loc) · 1.56 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<map>
/*
Maps are associative containers that store elements in a mapped fashion.
Each element has a key value and a mapped value.
No two mapped values can have same key values.
*/
using namespace std;
int main()
{
map<string,int> m;
m.insert(pair <string,int> ("Anish",10));
m.insert(pair <string,int> ("mrinal",2));
m.insert(pair <string,int> ("Vaibhav",3));
m.insert(pair <string,int> ("Rasila",4));
//can also insert using
// m.insert(make_pair(10,"Lucky"));
string name;
int marks;
cin>>name;
cin>>marks;
map<string,int>::iterator i=m.find(name);
int new_marks = (i->second + marks);
i->second = new_marks;
map<string,int>::iterator it;
//can also do random access using the key value as subscript-O(1)
cout<<m["Anish"]<<endl; //prints mrinal
//traversing the map using iterator
for(it = m.begin(); it != m.end();it++)
{
cout<<it->first;
cout<<" | ";
cout<<it->second;
cout<<endl;
}
cout<<"Erasing--"<<endl;
//will erase elements from begin of map till 2nd element , not 3
// remove all elements up to element with key = 3 in found
// m.erase(m.begin(), m.find(3) );
//
// for(i = m.begin(); i != m.end();i++)
// {
// cout<<i->first;
// cout<<" | ";
// cout<<i->second;
//
// cout<<endl;
// }
//find() method returns an iterator to key passed as argument
// map<int,string>::iterator i1;
// int n;
// cin>>n;
// i1 = m.find(n);
// if(i1->first)
// cout<<i1->second; //prints "mrinal"
//
// else
// cout<<"Not found"<<endl;
//
//
//
// return 0;
}