question about string hash function

Discussion in 'Programmer's Corner' started by moslem, Jul 10, 2010.

  1. moslem

    moslem Thread Starter New Member

    Joined:
    Dec 16, 2009
    Messages:
    20
    Hello every one,
    i've a string hash function it's performace is already good but there is some things that happens inside it and idon't know why
    the hash function:
    unsigned int table::hash(const string &word)
    {
    int hash = 0;
    int n=word.length();
    for(int i=0;i<n;i++)
    hash = 31*hash+word;
    return hash % MAX_TABLE;
    }
    my questions about it
    why we multiply by 31 the hash every time is this for making weighting for the characters or any thing else?
    thanks inadvance.
  2. Harrington

    Harrington New Member

    Joined:
    Dec 19, 2009
    Messages:
    86
    Location:
    London
    moslem likes this.
  3. Papabravo

    Papabravo AAC Fanatic!

    Joined:
    Feb 24, 2006
    Messages:
    6,258
    Location:
    Michigan, USA (GMT-5)
    moslem likes this.
Similar Threads
Forum Title Date
Programmer's Corner Question in Char and String Aug 11, 2013
Programmer's Corner Lisp/Scheme/Racket question: Convering string to procedure call. Aug 29, 2012
Programmer's Corner C++ string question Mar 12, 2008
Programmer's Corner basic question on programming ? Nov 17, 2014
Programmer's Corner PIC16F877A using MBLAB IDE (HITECH C Compiler) Beginner Question Nov 8, 2014

Share This Page