Maps-STL

Sort by

recency

|

328 Discussions

|

  • + 0 comments

    A clean version, with formatting revised using GPT. Written by a C++ beginner — any advice is welcome.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    using namespace std;
    
    
    int main() {
        int q, t;
        string x;
        
        cin >> q;
        
        map <string, int>m;
        while (q -- ){
            cin >> t >> x ;
            auto itr = m.find (x);
            if (t == 1){
                int k;
                cin >> k;
                m[x]+=k;
            }else if (t == 2) m.erase(x);
            else cout << (itr!=m.end()?itr -> second: 0) << "\n";
        }   
        return 0;
    }
    
  • + 0 comments
    int main() {
        map<string, int> marks;
        int count; cin >> count;
        
        for (int x = 0, query; x < count; x++)
        {
            map<string, int>::iterator pointer;
            string name;
            int mark = 0;
            
            cin >> query >> name;
            
            switch (query)
            {
                case 1: 
                {
                    cin >> mark;
                    if ((pointer = marks.find(name)) == marks.end())
                    {
                        marks.insert(make_pair(name, mark));
                    } else 
                    {
                        marks[name] += mark;
                    }
                    break; 
                }
                case 2:
                {
                    if ((pointer = marks.find(name)) == marks.end()) break;
                    marks.erase(pointer->first);
                    
                    break;
                }
                case 3:
                {
                    if ((pointer = marks.find(name)) == marks.end()) cout << 0 << endl;
                    else cout << pointer->second << endl;
                    break;    
                }
            }
        }
        
        return 0;
    }
    
  • + 0 comments

    Guys, the solution is easier than you think.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <string>
    using namespace std;
    
    
    int main() {
    
        std::map<std::string, int>  m;
        int q;
        int op; 
        int y;
        std::string x;
        
        std::cin >> q;
        
        for (int i=0; i<q; i++) {
            std::cin >> op;
            switch (op) {
                
                case 1:
                std::cin >> x >> y;
                m[x] += y;
                break;
                
                case 2:
                std::cin >> x;
                m[x] = 0;
                break;
                
                case 3:
                std::cin >> x;
                std::cout << m[x] << endl;
                break;                      
            }
            
        }
    		return 0;
    	}
    

    There is no need to use weird methods or even the iterator. Just a simple switch case! Add the marks if it's 1, set them to 0 if it's a 2, print whatever you have if it's 3. No need to "check" if the mark is 0 or not, or erase anything.

  • + 0 comments

    It really helps reinforce understanding of key operations like insertion, lookup, and iteration over key-value pairs. Betting Exchange Login ID and Password

  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int q; cin >> q;

    map<string,int> m;
    
    while(q--) {
        int y;
        string x;
        cin >> y >> x;
    
        if (y == 1) {
            int marks;
            cin >> marks;
            m[x] += marks;
        }
        else if (y == 2) {
            m.erase(x);
        }
        else if (y == 3) {
            cout << m[x] << endl;
        }
    }  
    return 0;
    

    }