Thread: Hashes?

  1. #1

    Hashes?

    Does C++ have any type of hash? I know in ruby you could create hashes but does C++ have this? And if it does can you show me an example of how to use it? If not then is there something similar to a hash?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    C++ has no hash table. The map class is similiar, but it's not hash based (lookups are slower, but it keeps the data ordered, note how the output is printed in alphabetical order). I can almost guarentee hash maps will be in the next version of the standard, and it's a pretty big lack IMO.

    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    int main() {
      std::map<std::string, int> nameToNum;
      nameToNum["one"] = 1;
      nameToNum["two"] = 2;
      nameToNum["three"] = 3;
      nameToNum["four"] = 4;
    
      std::cout << nameToNum["three"] << std::endl;
    
      for (std::map<std::string,int>::iterator i = nameToNum.begin(); 
           i != nameToNum.end();
           ++i) {
        std::cout << i->first << ' ' << i->second << std::endl;
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I may be in the minority, but I don't really see the point in hash tables. If you have ot use a hash table then you can probably recode it to be more efficient. But that is just my opinion.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Recode what? If you want to search fast, hash tables work nicely.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Munkey01, it is a matter of time.

    Programming should be efficient. For example, I *could* write my own more efficient I/O classes, but it will take time which can be spent elsewhere.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Thanks guys. I just wanted to know because I know when i used Ruby they came in handy.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>If not then is there something similar to a hash?
    You can make your own hash with a vector and list pretty easily.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    
    using namespace std;
    
    const int hsize = 101;
    
    int my_hash(string str)
    {
      int h = 0;
      string::iterator iter;
    
      for (iter = str.begin(); iter != str.end(); ++iter)
      {
        h = *iter + 31 * h;
      }
    
      return h % hsize;
    }
    
    string my_lookup(vector<list<string> >& table, string& item)
    {
      list<string> tlist = table[my_hash(item)];
      list<string>::iterator iter;
    
      for (iter = tlist.begin(); iter != tlist.end(); ++iter)
      {
        if (*iter == item)
        {
          return *iter;
        }
      }
    
      return ":nonexistant:";
    }
    
    int main()
    {
      string word;
      vector<list<string> > table(hsize);
    
      table[my_hash("now")].push_front("now");
      table[my_hash("is")].push_front("is");
      table[my_hash("the")].push_front("the");
      table[my_hash("time")].push_front("time");
    
      cout<<"Enter a word to find: "<<flush;
      getline(cin, word);
      cout<<"Found -- "<< my_lookup(table, word) <<endl;
    }
    *Cela*

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    STLport includes the common extention hash_map, it's worthwile for the debug mode alone. As others have said this will probably make it to the next version of the standard. I still recomend writing your own, builds character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putting several hashes together. Better than md5/sha1?
    By idleman in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2009, 12:42 PM
  2. Hashes in c
    By cosmo1996 in forum C Programming
    Replies: 4
    Last Post: 08-08-2007, 09:32 PM
  3. Pointers and hashes
    By chinook86 in forum C Programming
    Replies: 4
    Last Post: 04-04-2007, 01:01 AM
  4. Hashing, indexing and phonetic normalization for approximate str matching...
    By biterman in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-21-2006, 09:42 AM
  5. how to create hashes
    By B-Con in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-26-2005, 02:13 AM