Maps-STL

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    int main() {
        map<string, int> students;
        int studentNo, opCode;
        string studentName;
        cin >> studentNo;
        while (studentNo-- > 0) {
            cin >> opCode >> studentName;
            switch(opCode){
                case 1:
                    cin >> opCode;
                    students[studentName] = students[studentName] + opCode;
                    break;
                case 2:
                    students.erase(studentName);
                    break;
                case 3:
                    cout << students[studentName] << '\n'; // Map will create student and assign a default value to marks even if he/she doesn't exist (unlike in Python where it will throw an error)
                    break;
            }
        }
        
        return 0;
    }