Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How can I load a txt file into a hashing table in C++?

I'm doing a programming project using hashing in C++

it's supposed to load the txt file and get a list of ID numbers with names. Looks like this:

1234 Mary Got

1345 Jane Doe

1111 Jose Alvares

1112 James Great

So when I try loading it into the hashing table, it doesn't work. I created a structure that has a string for ID and a string for the name. What's the easiest way to load the list into the Hash table which consists of arrays of the structure?

Any help is greately appriciated!

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    include <iostream>

    #include <string>

    #include <fstream>

    #include <map>

    using std::cout;

    using std::string;

    using std::ifstream;

    using std::map;

    int main()

    {

    map<int, string> table;

    int t = 0;

    string s;

    ifstream file("filename.txt");

    while(file >> t && getline(file, s))

    table[t] = s;

    map<int, string>::const_iterator iter = table.begin();

    for(; iter != table.end(); ++iter)

    cout << iter->first << iter->second << "\n";

    }

    Is not really a Hash table, a map is implemented using a red black tree, but if you have a hash table in your implementation, it can be almost the same

    see unordered_map

  • ?
    Lv 4
    4 years ago

    the place is the txt document coming from? If that's from the server, then Adz is right, AJAX is a thank you to bypass - the document can in basic terms be served via way of the conventional webserver in keeping with an AJAX request. If that's on yet another server, you are able to run into the browser's go-website scripting risk-free practices. If the document is on the customer's close by gadget, it is not accessible. Javascript does not have get right of entry to to the close by filesystem, for risk-free practices motives.

Still have questions? Get your answers by asking now.