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 .



  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <set>
  6. #include <map>
  7. #include <algorithm>
  8. using namespace std;
  9. int main()
  10. {
  11. map<string,int>s;
  12. int q;
  13. cin>>q;
  14. for(int i=0;i<q;i++)
  15. {
  16. int t,n,m;
  17. cin>>t;
  18. switch(t)
  19. {
  20. case 1:
  21. {
  22. string name;
  23. int marks;
  24. cin>>name>>marks;
  25. map<string,int>::iterator itr=s.find(name);
  26. if(itr==s.end())
  27. s.insert(make_pair(name,marks));
  28. else
  29. itr->second+=marks;
  30. break;
  31. }
  32. case 2:
  33. {
  34. string name;
  35. cin>>name;
  36. s.erase(name);
  37. break;
  38. }
  39. case 3:
  40. {
  41. string name;
  42. cin>>name;
  43. map<string,int>::iterator itr=s.find(name);
  44. if(itr==s.end())
  45. cout<<"0"<<endl;
  46. else
  47. cout<<itr->second<<endl;
  48. break;
  49. }
  50. }
  51. }
  52. return 0;
  53. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube