7

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?

Greg
  • 8,175
  • 16
  • 72
  • 125
  • I suspect you want to enforce some policy if it is the case, then ask the unordered_map to enforce it don't try to force it from client code. Check if max_load_factor member function solves the underling question. – Giuseppe Puoti Sep 10 '17 at 06:59

1 Answers1

11

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;
    }
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Aerle
  • 136
  • 1
  • 3