C++ boost unordered_map - 确定容器中是否存在键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7213211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:36:42  来源:igfitidea点击:

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

c++boost

提问by gewizz

In boost::unordered_maphow 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?

我使用的是自定义值类型还是其他问题?

回答by Luká? Lalinsky

You can use the findmethod:

您可以使用以下find方法:

if (my_hash_map.find(non-existent key) == my_hash_map.end())

回答by dalle

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