c++ boost unordered_map -确定key是否存在于容器中

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

本文关键字:是否 key 存在 于容器 确定 boost unordered map c++      更新时间:2023-10-16

boost::unordered_map中,我如何确定其中是否存在键?

boost::unordered_map<vector<int>, MyValueType> my_hash_map;
if (my_hash_map[non-existent key] == NULL)

上面得到编译错误"no match for operator '=='…"

问题是我使用自定义值类型或其他东西?

您可以使用find方法:

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

对于任何关联容器,exist()都拼写为count():

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