In boost::unordered_map
how do I determine if a key exists in it or not?
boost::unordered_map<vector<int>, MyValueType> my_hash_map; if (my_hash_map[non-existent key] == NULL)
The above gets compiler error "no match for operator '=='..."
Is the problem that I am using a custom value type or something else?
Using unordered_map::count function If we only want to know the presence of a key in the map container but doesn't want an iterator to it, we can use the count() member function of the map container, which returns the value of 1 if the specified key is found, or 0 if the key is not found.
Use the std::map::contains Function to Check if Key Exists in a C++ Map. contains is another built-in function that can be used to find if the key exists in a map . This function returns a boolean value if the element with the given key exists in the object.
Return values: If the given key exists in unordered_map it returns an iterator to that element otherwise it returns the end of the map iterator. // find function in unordered_map. Time Complexity : O(1) on average.
You can use the find
method:
if (my_hash_map.find(non-existent key) == my_hash_map.end())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With