本文介绍了提高:: hash_combine VS简单的异或运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Boost库,建设中的作用的boost :: hash_combine 是这样的:

When using the boost library, the fuction boost::hash_combine works like this:

seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);

http://www.boost.org/doc/libs/1_46_1/doc/html/hash/reference.html#boost.hash_combine

这是什么做法VS只是异或的优势在哪里?

What is the advantage of this approach vs simply XOR-ing?

使用异或,人们甚至可以使用散列函数使用无序容器的钥匙,而这个人是有顺序的。

With XOR-ing, one can even use the hash function to use unordered containers as keys, while this one is order dependent.

推荐答案

有很多有序的容器,如清单。如果你会使用XOR,那么你基本上会说, [0,1] 相同 [1,0] 。这显然​​不是这样。它更容易覆盖的方法比强加一个将创造大量的碰撞有序无序那些容器。 XOR有很多其他讨厌的属性。例如,如果您有重复的元素,然后他们会互相抵消。

There are many ordered containers such as lists. If you would use XOR then you would basically say that [0, 1] is the same as [1, 0]. That's obviously not the case. It's much easier to override the method for unordered containers than to impose one that will create a lot of collisions for ordered ones. XOR has a lot of other nasty properties. For instance, if you have duplicate elements then they will cancel each other out.

在月底的哈希的想法是要合理确定你没有得到多个元素的值相同。 XOR本身并不适合该属性。

In the end the idea for a hash is to be reasonably sure that you don't get the same value for multiple elements. XOR doesn't fit that property by itself.

这篇关于提高:: hash_combine VS简单的异或运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:58