Thread: hashing a string to an int question

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    hashing a string to an int question

    Hi guys,

    I am trying to make a hashing function that will accept a string and output an integer. On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.

    Code:
    char string[] = "Bob Jones";
    
    int hash ( const char *string ) {
      int result = 0;
      while ( *string ) result += *string++;
      return result;
    }

    It works fine for using character array stings, however, I would prefer to use a string declared as..

    Code:
    string name = "Bob Jones";
    Is there anyway to accomplish the same thing using strings?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    hash(name.str())?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    So the string is a class that has a member function named str?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    name.c_str() instead
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Quote Originally Posted by Mario F.
    name.c_str() instead
    Ah! Is the general structure of the string class posted anywhere? I would like to familiarize myself with it's member functions/data members (I'm still kind of new to this).

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    http://www.cppreference.com/ is an excellent quick resource. And downloadable too!

    EDIT: Although not always complete.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.
    I remember that. I also seem to remember that he mentioned how awful it is. Not only is the algorithm poor for hashing, it's also dangerous because it's so very easy to overflow an int. Here's a better one:
    Code:
    unsigned hash ( const char *key )
    {
      unsigned h = 0;
    
      for ( int i = 0; key[i] != '\0'; i++ )
        h = 33 * h + key[i];
    
      return h;
    }
    >Is there anyway to accomplish the same thing using strings?
    Code:
    int h = hash ( name.c_str() );
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Sweet! Just what I needed. Thanks Mario F.!

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    Smile

    Quote Originally Posted by Prelude
    >On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.
    I remember that. I also seem to remember that he mentioned how awful it is. Not only is the algorithm poor for hashing, it's also dangerous because it's so very easy to overflow an int. Here's a better one:
    Code:
    unsigned hash ( const char *key )
    {
      unsigned h = 0;
    
      for ( int i = 0; key[i] != '\0'; i++ )
        h = 33 * h + key[i];
    
      return h;
    }
    >Is there anyway to accomplish the same thing using strings?
    Code:
    int h = hash ( name.c_str() );
    Thanks Prelude,

    I did modify the code from it's original form. I simply posted it like that because it was simpler. Thank you for the suggestions, they've given me some new ideas.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Thank you for the suggestions, they've given me some new ideas.
    Just a note, hashing is something you really want to avoid experimenting on in production code. It's really hard to come up with a good algorithm and really easy to come up with a truly awful algorithm.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM