Thread: hash_table IfContainsKey function?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    hash_table IfContainsKey function?

    I am using the hash_table as a simple dictionary type thing. But my problem is this container doesn't have a IfContainsKey function so I can easily check if a key already exists in it. How would I go about doing this type of check?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I found out how. Simply use the count function in the hash_map. That returns the number of keys that matched the key you specify. The name count made me think it returns the number of elements in the container.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Rune Hunter View Post
    I am using the hash_table as a simple dictionary type thing. But my problem is this container doesn't have a IfContainsKey function so I can easily check if a key already exists in it. How would I go about doing this type of check?
    Code:
    if(map.find(key) == map.end())
    {
        // No such key
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no standard hash_table, so make sure to indicate your compiler and standard library implementation when you have questions about it. Different libraries have very different versions of hash tables.

    You might consider using the TR1 unordered_map instead, since that is quasi-standard already and should be accepted into the next standard when it is done. That means it is much more likely that it will work on other compilers and it is more likely that other programmers will be familiar with it as well.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ahh okay. Well seeing as to how I fixed my problem and the hash table will be invisible to other users I think I'll stick with the MS hash_table for now. But I'll keep the TR1 unordered_map in mind for future projects. Thanks.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Rune Hunter View Post
    Ahh okay. Well seeing as to how I fixed my problem and the hash table will be invisible to other users I think I'll stick with the MS hash_table for now. But I'll keep the TR1 unordered_map in mind for future projects. Thanks.
    Is there any reason to believe that std::map won't be fast enough?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM