Why does the hash function return size_t and how is it used?

I understand the mathematical basis for hash tables. I have a hash function (which 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;
}
  • Why does this return size_t?
  • How to use this to write store()that places a row in a hash table?
  • How can this be adapted for an array of characters?
  • As for No. 3, would it be advisable to replace a loop forwith a loop whilethat ends with a symbol '\0'?

FYI, I'm studying for a second job interview, and that’s why I ask.

+5
source share
3 answers
  • size_t, ( ). - ?

  • ""? ? -, , . (: "".)

  • ?

  • , ?

+2
  • size_t, , , , .

  • - - "" . , , 0 SIZE-1, . " ", , , , , .

  • , .

  • NUL, . , , . strlen().

Ps. const string &s ++, C. .

+1

stringcontains its own length; you do not need to pass it. This is C ++, not C-links in C. There is no need for strlenor something like that, or NULLterminators, or any such. This means that replacing it with a while loop, the seeker \0, will be Bad ™, since there is no guarantee that it std::stringeven has one, not to mention it as a terminator.

+1
source

All Articles