[英]Why C++ hash functions' return type is std::size_t, instead of a platform-independent type?
As shown here , std::hash
template provided in the standard library will return a value of type std::size_t
. 如图所示这里 ,
std::hash
标准库提供的模板将返回一个类型的值std::size_t
。 However, the exact type of std::size_t
is platform-dependent and implementation-dependent, make it impossible to estimate the collision probability. 但是,
std::size_t
的确切类型与平台有关且与实现有关,因此无法估计碰撞概率。
I cannot see any advantage of returning std::size_t
, so my problem is, why does not the hash function return a value of fixed type instead? 我看不到返回
std::size_t
任何优势,所以我的问题是,为什么哈希函数不返回固定类型的值?
The intended purpose of std::hash
is for implementation of a hash table, and specifically the unordered associative containers of the standard library. std::hash
的预期目的是实现哈希表,尤其是标准库的无序关联容器。 A hash table is an array, and std::size_t
is an appropriate choice, since it is the type that is guaranteed to be able to represent all indices of any array. 哈希表是一个数组,而
std::size_t
是一个适当的选择,因为它是保证可以表示任何数组的所有索引的类型。 While not actually specified by the standard, in practice those standard containers are likely to use std::size_t
as the size type. 尽管标准并未对此进行实际规定,但实际上这些标准容器可能
std::size_t
用作大小类型。
If you need a hash function for inter-system communication, std::hash
is not a good choice. 如果需要散列函数进行系统间通信,则
std::hash
不是一个好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.