boost::unordered_map
如何確定密鑰是否存在於其中?C++ boost unordered_map - 確定密鑰是否存在於容器中
boost::unordered_map<vector<int>, MyValueType> my_hash_map;
if (my_hash_map[non-existent key] == NULL)
以上得到編譯器錯誤「敵不過運營商‘==’......」
是,我使用自定義的值類型還是其他什麼問題?
boost::unordered_map
如何確定密鑰是否存在於其中?C++ boost unordered_map - 確定密鑰是否存在於容器中
boost::unordered_map<vector<int>, MyValueType> my_hash_map;
if (my_hash_map[non-existent key] == NULL)
以上得到編譯器錯誤「敵不過運營商‘==’......」
是,我使用自定義的值類型還是其他什麼問題?
可以使用find
方法:
if (my_hash_map.find(non-existent key) == my_hash_map.end())
exist()
拼寫count()
任何associative container:
if (my_hash_map.count(key)) { /*key exist*/ }
if (!my_hash_map.count(key)) { /*key does not exist*/ }
......就像任何符合標準的容器,順便說一句。 – xtofl
快速跟進,是否仍然在O(1)中完成?謝謝! – gewizz
我假設是這樣,因爲unordered_map是一個散列表。 –