How to identify whether or not std::unordered_map has experienced hash collisions?

笑着哭i 提交于 2020-07-03 06:50:20

问题


How to identify whether or not the keys in a std::unordered_map have experienced hash collisions?

That is, how to identify if any collision chaining is present?


回答1:


You can use the bucket interface and its bucket_size method.

std::unordered_map<int, int> map;
bool has_collision = false;

for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) {
    if(map.bucket_size(bucket) > 1) {
        has_collision = true;
        break;
    }
}


来源:https://stackoverflow.com/questions/46137811/how-to-identify-whether-or-not-stdunordered-map-has-experienced-hash-collision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!