Home > Back-end >  What's the difference b/w map[key] vs map.count(key) ? (particularly in this code)
What's the difference b/w map[key] vs map.count(key) ? (particularly in this code)

Time:11-07

So I was Solving a leetcode Problem and one dumb mistake made in debugg for more than an hour. Leetcode Question

Answers(both working and not working) Working code: `

class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        unordered_map<string,int> map;
        for(string s:words){
            map[s]  ;
        }
        bool isOdd = false;
        int ans = 0;
        for(auto i:map){
            string rv = i.first;
            reverse(rv.begin(),rv.end());
            if(i.first[0]==i.first[1]){
                if(i.second%2==0)
                    ans =i.second;
                else{
                    ans = i.second-1;
                    isOdd = true;
                }
            }
            else if(i.first[0]<i.first[1] && map.count(rv)){
                ans  = 2*min(i.second,map[rv]);
            }
        }
        if(isOdd){
            ans  ;
        }
        return 2*ans;      
    }
};

Not working code: `

class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        unordered_map<string,int> map;
        for(string s:words){
            map[s]  ;
        }
        bool isOdd = false;
        int ans = 0;
        for(auto i:map){
            string rv = i.first;
            reverse(rv.begin(),rv.end());
            if(i.first[0]==i.first[1]){
                if(i.second%2==0)
                    ans =i.second;
                else{
                    ans = i.second-1;
                    isOdd = true;
                }
            }
            else if(i.first[0]<i.first[1] && map[rv]){
                ans  = 2*min(i.second,map[rv]);
            }
        }
        if(isOdd){
            ans  ;
        }
        return 2*ans;      
    }
};

The only difference between both code is map[rv] ==> map.count(rv)

test case which is giving error:

["oo","vv","uu","gg","pp","ff","ss","yy","vv","cc","rr","ig","jj","uu","ig","gb","zz","xx","ff","bb","ii","dd","ii",
"ee","mm","qq","ig","ww","ss","tt","vv","oo","ww","ss","bi","ff","gg","bi","jj","ee","gb",
"qq","bg","nn","vv","oo","bb","pp","ww","qq","mm","ee","tt","hh","ss","tt","ee","gi","ig","uu","ff","zz",
"ii","ff","ss","gi","yy","gb","mm","pp","uu","kk","jj","ee"]

Can anyone please help me?

I've tried googling this stuff but couldn't find it. then i've tried asking few people on discord. but no progress. I just wanna know why above(not working part) code in not working. What is the deal with map[key] and map.count(key)? When should i use which one?

CodePudding user response:

std::map.count() will check, if an element with a given key exists. It will not modify the container. Please see here. It is even defined as constto indicate that fact.

The std::maps index operator is different. It

returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

So, if the key does not exist, it will add an entry to the map, with this new key.

Therefore your second code cannot work.

CodePudding user response:

map[rv] isn't a regular getter. It inserts default element if key is not found.

so you mutate (unordered_map) map whereas you iterate on it (which (might) invalidate iterator, making you loop undefined behavior).

  • Related