-
Notifications
You must be signed in to change notification settings - Fork 9
/
map.cpp
48 lines (46 loc) · 1.4 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
// https://www.hackerrank.com/challenges/cpp-maps/problem?isFullScreen=false&h_r=next-challenge&h_v=zen
#include <map>
#include <iostream>
#include <utility>
using namespace std;
int main() {
map<string, int> marks;
long int test_cases, grades, query;
string student_name;
cin >> test_cases;
while(test_cases--)
{
cin >> query >> student_name;
if(query == 1)
{
// Store student name and grade
cin >> grades;
// If the user exist, add the new record, otherwise, update current value
// Print the marks of the student if he/she exists, 0 otherwise
map<string, int>::iterator itr = marks.find(student_name);
if(itr == marks.end())
marks.insert(make_pair(student_name, grades));
else
{
itr->second = itr->second+grades;
}
}
else if(query ==2)
{
// Remove marks and name of the student
marks.erase(student_name);
}
else if(query ==3)
{
// Print the marks of the student if he/she exists, 0 otherwise
map<string, int>::iterator itr = marks.find(student_name);
if(itr == marks.end())
cout <<0<<endl;
else
{
cout<<itr->second<<endl;
}
}
}
return 0;
}