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-exis开发者_JS百科tent 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?
You can use the find
method:
if (my_hash_map.find(non-existent key) == my_hash_map.end())
exist()
is spelled count()
for any associative container:
if (my_hash_map.count(key)) { /*key exist*/ }
if (!my_hash_map.count(key)) { /*key does not exist*/ }
精彩评论