Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ boost unordered_map - determine if key exists in container

Tags:

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?

like image 415
gewizz Avatar asked Aug 27 '11 07:08

gewizz


People also ask

How do you check if an element is present in an Unordered_map?

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.

How do you find if a key exists in a map C++?

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.

What is the complexity of find in Unordered_map?

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.


1 Answers

You can use the find method:

if (my_hash_map.find(non-existent key) == my_hash_map.end()) 
like image 145
Lukáš Lalinský Avatar answered Oct 27 '22 15:10

Lukáš Lalinský