Finding value in unordered_map

前端 未结 4 1480
忘了有多久
忘了有多久 2021-01-11 14:49

I am using Boost unordered_map. I have a key value pair for each entry. How could I determine whether a particular value exist in the map? (I don\'t want to create another u

4条回答
  •  孤城傲影
    2021-01-11 15:51

    Why can't we use count method instead of find()

    Description: Count elements with a specific key Searches the container for elements whose key is k and returns the number of elements found. Because unordered_map containers do not allow for duplicate keys, this means that the function actually returns 1 if an element with that key exists in the container, and zero otherwise.

    unordered_map hash;
        //converted array into hashMap
        for(int i=0; i<6; i++)
        {
            hash[i];
        }
    
        //commom elemenest value is set to 1 in hashMap
        for(int i =0; i<7; i++)
        {
            //element exist in array1
            if(hash.count(i))
            {
                hash[i] = 1;
            }
        }
    

提交回复
热议问题