In this HackerRank Maps-STL problem in c++, you will be given Q queries. each query is of one of the following types.
  1. Add the marks to the student whose name is X.
  2. Erase the marks of the students whose name is X.
  3. Print the marks of the students whose name is X.

HackerRank Maps-STL in C++ problem solution

 

HackerRank Maps-STL in C++ problem solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int q;
    cin >> q;
    
    const int Q = q;
    
    map<string, int> m;
    
    for (int i = 0; i < Q; i++) {
        int type;
        cin >> type;
        
        string name;
        int mark;
        map<string, int>::iterator it;
            
        switch (type) {
            case 1:
            cin >> name;
            cin >> mark;
            it = m.find(name);
            if (it == m.end()) {
                m.insert(pair<string,int>(name, mark));
            } else {
                m.erase(name);
                m.insert(pair<string,int>(name, mark + it->second));
            }
            break;
            
            case 2:
            cin >> name;
            m.erase(name);
            break;
            
            case 3:
            cin >> name;
            it = m.find(name);
            cout << (it == m.end() ? 0 : it->second) << endl;
            break;
        }
    }
    
    return 0;
}