简体   繁体   中英

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

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?

您可以使用find方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM