Maps-STL

Sort by

recency

|

314 Discussions

|

  • + 0 comments
    map<string,int> studentMarks;
    int n,query,marks;
    cin >> n;
    string name;
    
    for(int i=0; i<n; ++i){
        cin >> query;
    
        if(query == 1){
            cin >> name >> marks;
            studentMarks[name] += marks;
        }
        else if(query == 2){
            cin >> name;
            studentMarks.erase(name);
        }
        else if(query == 3){
            cin >> name;
            if(studentMarks.find(name) != studentMarks.end()){
                cout << studentMarks[name];
            }
            else{
                cout << 0;
            }
        }
    }   
    
  • + 0 comments
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int q_no, q_type, marks;
        string name;
        map<string, int> m;
        map<string, int>::iterator it;
        
        cin >> q_no;
        
        while(q_no){
            cin >> q_type;
            switch(q_type){
                case 1:
                    cin >> name >> marks;
                    it = m.find(name);
                    if(it != m.end()){
                        m[name] += marks;
                    }
                    else{
                        m.insert(make_pair(name, marks));    
                    }               
                    break;
                case 2:
                    cin >> name;
                    m[name] = 0;
                    break;
                case 3:
                    cin >> name;
                    it = m.find(name);
                    (it != m.end()) ? cout<< m[name] << endl : cout<< 0 << endl;
                    break;
            }
            
            q_no--;
        }
        
        return 0;
    }
    
  • + 0 comments
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int n;
        int query;
        map<string, int> students;
        cin>>n;
        string name;
        int marks;
        for(int i=0;i<n;i++)
        {
            cin>>query;
            if(query==1)
            {
                cin>>name>>marks;
                students[name]+=marks;
            }
            if(query==2 || query==3)
            {
                cin>>name;
            }
            switch(query)
        {
            case 1: 
            students.insert(make_pair(name,marks)); 
            break;
            
            case 2:
            students.erase(name);
            break;
            
            case 3:
            cout<<students[name]<<"\n";
            break;
        }
        
    }
        return 0;
    }
    
  • + 0 comments

    Here is - hackerrank maps - stl problem solution

  • + 0 comments
    #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; 
    
        map<string, int> mp;
        for (int i = 0; i < q; i++) {
            int type;
            cin >> type;
    
            if (type == 1) {
                string name;
                int marks;
                cin >> name >> marks;
                mp[name] += marks;
            } else if (type == 2) {
                string name;
                cin >> name;
                mp.erase(name);
            } else if (type == 3) {
                string name;
                cin >> name;
    
                cout << mp[name] << endl;
            }
        } 
        return 0;
    }