为什么哈希函数返回 size_t,它是如何使用的?

发布于 2024-11-16 04:39:00 字数 736 浏览 1 评论 0原文

我了解哈希表的数学基础。我在下面有一个哈希函数(我在某处找到的):

/* Fowler / Noll / Vo (FNV) Hash */
static const size_t InitialFNV = 2166136261U;
static const size_t FNVMultiple = 16777619;
size_t myhash(const string &s, int length)
{
    size_t hash = InitialFNV;
    for(size_t i = 0; i < length; i++)
    {
        //XOR the lower 8 bits
        hash = hash ^ (s[i]);

        //Multiply by the multiple
        hash = hash * FNVMultiple;
    }
    return hash;
}
  1. 为什么它返回 size_t
  2. 如何用它来写一篇 store() 函数放置一个 哈希表中的字符串?
  3. 这如何适用于数组 字符数?
  4. 关于#3,是否合适替换 for 循环和 while 循环 以 '\0' 字符终止?

仅供参考,我正在为第二次工作面试做准备,这就是我问这个问题的原因。

I understand the mathematical basis for hash tables. I have a hash function (that I found somewhere) below:

/* Fowler / Noll / Vo (FNV) Hash */
static const size_t InitialFNV = 2166136261U;
static const size_t FNVMultiple = 16777619;
size_t myhash(const string &s, int length)
{
    size_t hash = InitialFNV;
    for(size_t i = 0; i < length; i++)
    {
        //XOR the lower 8 bits
        hash = hash ^ (s[i]);

        //Multiply by the multiple
        hash = hash * FNVMultiple;
    }
    return hash;
}
  1. Why does this return a size_t?
  2. How would one use this to write a
    store() function which places a
    string in a hash table?
  3. How can this be adapted for an array
    of characters?
  4. In regards to #3, would it be appropriate to replace
    the for loop with a while loop that
    terminates at the '\0' character?

FYI, I am studying up for a second job interview, and that's why I'm asking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骑趴 2024-11-23 04:39:00
  1. 它返回size_t,因为这是本机整数(也是最快的)。为什么选择其他东西?

  2. “桌子”?哪张桌子?如果您指的是哈希表,那么您可以使用返回值来选择一个随机存储桶来放入对象。(提示:想想“余数”。)

  3. 它不是已经适合数组了吗?

  4. 如果它是一个以 null 结尾的字符串,为什么不呢?

  1. It returns size_t because that's the native integer (also fastest). Why choose anything else?

  2. "The table"? Which table? If you mean a hashtable, then you can use the return value to choose a random bucket to put the object in. (Hint: Think "remainder".)

  3. Isn't it already adapted for an array?

  4. If it's a null-terminated string, why not?

生生漫 2024-11-23 04:39:00
  1. 它不一定是 size_t,但它可能应该是无符号整数类型,因此 mod 是明确定义的。

  2. 通常的方法是使用哈希函数将“key”数据转换为数组索引。因此,您可以对数组的大小进行取模,以获得一个从 0 到 SIZE-1 的整数,您可以将其用作索引。您还需要一个“冲突解决策略”,因为除非哈希产生完美的结果,否则某些不同的键对将哈希为相同的值。

  3. 它似乎已经如此适应。

  4. 如果字符串以NUL结尾,则可以搜索空值。但是编写的函数传递了一个长度作为参数。最简单的方法是保留工作函数,并使用 strlen() 的结果调用它。

诗。 const string &s 表示 C++,不是 C。这在编译时可能很重要。

  1. It doesn't have to be size_t, but it should probably be an unsigned integer type so mod is well-defined.

  2. The usual way is to use the hash function to transform the 'key' data into an array index. So you mod by the size of the array to get an integer from 0 to SIZE-1 that you can use as an index. You'll also need a "collision resolution strategy" because unless the hash yields perfect results, some pairs of keys which are different will hash to the same value.

  3. It appears already to be so adapted.

  4. If the string ends in NUL, you can search for the null. But the function as written is passed a length as argument. Simplest to leave the working function alone, and call it with the result of strlen().

Ps. the const string &s means C++, not C. This may be important when compiling.

你丑哭了我 2024-11-23 04:39:00

string 保留它自己的长度,您不需要传入它。这是 C++,而不是 C - C 中没有引用。不需要 strlen 或类似的东西that,或 NULL 终止符,或任何此类。这意味着用查找 \0 的 while 循环替换它将会是 Bad™,因为不能保证 std::string 甚至有一个,更不用说将其作为它是终结者。

string holds it's own length, you don't need it to be passed in. That's C++, not C- no references in C. There's no need for strlen or anything like that, or NULL terminators, or anysuch. That means that replacing it with a while loop looking for \0 would be Bad™, as there's no guarantee that std::string even has one, let alone has it as it's terminator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文