简体   繁体   English

C ++ boost unordered_map - 确定密钥是否存在于容器中

[英]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如何确定其中是否存在密钥?

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 : exist()拼写为任何关联容器的 count()

if (my_hash_map.count(key)) { /*key exist*/ }

if (!my_hash_map.count(key)) { /*key does not exist*/ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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