开发者

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

开发者 https://www.devze.com 2023-03-31 17:13 出处:网络
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;

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*/ }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号