简体   繁体   English

std::unordered_map 是否有可能发生冲突?

[英]Is there any possibility that std::unordered_map collides?

I seen a post in here that you could "meet with the Birthday problem."我在这里看到一个帖子,你可以“遇到生日问题”。 while using std::unordered_map使用std::unordered_map

When should I use unordered_map and not std::map 我什么时候应该使用 unordered_map 而不是 std::map

Which really surprises me, that is the same that saying std::unordered_map is unsafe to use.这真的让我感到惊讶,这与说std::unordered_map使用不安全是一样的。 Is that true?真的吗? If i'm not explaining myself, let me show you an example:如果我不解释自己,让我给你看一个例子:

unordered_map<size_t, size_t> m;
for (size_t i = 0; i < 100; ++i)
    m[i] = i;
for (size_t i = 0; i < 100; ++i)
    if (m[i] != i)
        cerr << "ERROR!" << endl;

If this code is in main , is there any possibility that it prints ERROR!如果此代码在main中,是否有可能打印ERROR! ? ?

Is there any possibility that std::unordered_map collides? std::unordered_map 是否有可能发生冲突?

It isn't the container that has collisions, but the hash function that you provide for the container.发生碰撞的不是容器,而是您为容器提供的 hash function。

And yes, all hash functions - when their output range is smaller than the input domain - have collisions.是的,所有 hash 函数 - 当它们的 output 范围小于输入域时 - 都会发生冲突。

is there any possibility that it prints ERROR??有没有可能打印错误?

No, it isn't possible.不,这是不可能的。 It's completely normal for the hash function to put multiple values into a single bucket. hash function 将多个值放入单个存储桶中是完全正常的。 That will happen in case of hash collision, but it also happens with different hash values.这将在 hash 碰撞的情况下发生,但它也会发生在不同的 hash 值的情况下。 The lookup function will get the correct value using linear search.查找 function 将使用线性搜索获得正确的值。 The identity of a key is determined by the equality function, not by the hash function.密钥的身份由等式 function 确定,而不是由 hash function 确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM