HackerRank C++ Maps-STL
You are appointed as the assistant to a teacher in a school and she is correcting the answer sheets of the students.Each student can have multiple answer sheets.So the teacher has Q queries:
1 XY :Add the marks to the student whose name is .
2 X: Erase the marks of the students whose name is X .
3 X: Print the marks of the students whose name is X. (If X didn't get any marks 0 print .
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <set>
- #include <map>
- #include <algorithm>
- using namespace std;
-
- int main()
- {
-
- map<string,int>s;
-
- int q;
- cin>>q;
-
- for(int i=0;i<q;i++)
- {
- int t,n,m;
- cin>>t;
- switch(t)
- {
- case 1:
- {
- string name;
- int marks;
- cin>>name>>marks;
- map<string,int>::iterator itr=s.find(name);
- if(itr==s.end())
- s.insert(make_pair(name,marks));
- else
- itr->second+=marks;
- break;
- }
- case 2:
- {
- string name;
- cin>>name;
- s.erase(name);
- break;
- }
- case 3:
- {
- string name;
- cin>>name;
- map<string,int>::iterator itr=s.find(name);
- if(itr==s.end())
- cout<<"0"<<endl;
- else
- cout<<itr->second<<endl;
- break;
- }
- }
- }
- return 0;
-
- }