how to use "std::hash_map"

This is a discussion on how to use "std::hash_map" within the C++ Programming forums, part of the General Programming Boards category; In my computer class, I was given an assignment to create a database using a hash table to store and ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    Exclamation how to use "std::hash_map"

    In my computer class, I was given an assignment to create a database using a hash table to store and retrieve names and phone numbers that is due tomorrow morning sharp. While reading more information about hash tables, I came across std::hash_map and would like to use it but I don't know how.

    Does anyone have any experience or know where I can find good documentation of how to use it?

    I'm already searching on the NET but can't come across anything useful.

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    Okay, I managed to find some documentation:
    http://msdn.microsoft.com/library/de...hmap_class.asp

    I do have some questions though and curious if someone out can help. For my programming assignment, I need to create a database that can hold names and phone numbers. There are two text files. One text file has a list of phone numbers and the other has the names. Using hash_map, how am I able to populate the hash table with this information?

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    hash_map is, unfortunetely, not standardized C++.

    Quick browsing of the MSDN shows that hash_map has the same interface as std::map. The following code should work interchangably with map and hash_map, assuming you change the include and typedef appropriately.

    What you want to do is read keys from one file, and values from another (you don't tell which way you want the search to work, I'd assume from person to phone number).


    Code:
    #include <map>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    typedef std::map<std::string, std::string> phonebook_t;
    
    int main() {
      phonebook_t phonebook;
    
      std::ifstream people_stream("people.txt");
      std::ifstream numbers_stream("numbers.txt");
    
      std::string person;
      std::string number;
    
      while (people_stream >> person && numbers_stream >> number) {
        phonebook[person] = number;
      }
      
      phonebook_t::iterator i;
      for (i = phonebook.begin(); i != phonebook.end(); ++i){
        std::cout << i->first << " -> " << i->second << std::endl;
      }
    
      // find Rob's number
      phonebook_t::iterator found = phonebook.find("Rob");
      if (found != phonebook.end()) {
        std::cout << "Rob's number is " << found->second << '\n';
      } else {
        std::cout << "Rob is not in phonebook\n";
      }
    
      return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21