vendredi 8 novembre 2019

Ambiguity in Hashmap?

I have used unordered map or hash map many times but this is the firsttime I am encountering problem like this : if I run this code :

string s  = "dvd";
for(int i = 0;i < s.size();i++){
        if(!map[s[i]]){
            cout<<s[i];
            map[s[i]] = i;
        }
    }

the output is

d v d

My question since already d is in map , so why the code printing it again. And another interesting thing is that when i run this code

    string s = "dvd";

    unordered_map<char,int>map;
    for(auto i :s){
        if(!map[i]){
            cout<<i<<" ";
            map[i] = i;
        }    
    }

it gives the expected output which is

d v

What is the thing I am missing here??

Aucun commentaire:

Enregistrer un commentaire