I was assigned to create a chained hash table using a vector of vectors. It is designed to hold objects of type Entry. I have written all of the functions and the constructor but when I try to use the constructor that reads an input and then output it back to me in order of keys, whichever string was on the last line of the .txt file its reading from is gone. I think its a problem with my constructor because when I try to use get to get that specific value from the table before printing it its gone. I was hoping for some insight to where I might be going wrong from someone with a little more experience. Thanks. Heres my code:
//
// table.h
//
//
#ifndef table_h
#define table_h
#include <string>
#include <vector>
#include <algorithm>
#include "entry.h"
usingnamespace std;
class Table {
public:
Table(unsignedint max_entries = 100);
//Builds empty table to hold 100 entries
Table(unsignedint entries, std::istream& input);
//Builds table to hold entries, reads and puts them 1 at a time
~Table();
//Destructor
void put(unsignedint key, std::string data);
//Creates new entry for the table
//Updates if key is used
void put(Entry e);
//Creates a copy of e in the table
string get(unsignedint key) const;
//Returns string associated with key
//Returns empty string if key isnt used
bool remove(unsignedint key);
//Removes entry in given key
//Returns true of removed, false of no entry
int find(Entry e);
//index in second array that e exists, 0 if not found
friend std::ostream& operator<< (std::ostream& out, const Table& t);
//Prints each entry in the table in order of their key
private:
int size;
int num_entries;
unsignedint hashkey(unsignedint key) const;
vector<vector<Entry> > A;
};
#endif /* table_h */
Line 12: It supposed to be the default constructor that sets the table size to 100, but I kept getting an error so I moved it into the function.
Line 22-25: The number of entries is specified by the user beforehand. Its not that generalized but that function definition was given is required for it to pass the automated tests.
And for the find function I dont use it anywhere I originally wrote it as a helper function and didnt end up using it. I just forgot to take it out.
Here is the code for Entry's input operator:
1 2 3 4 5 6 7 8 9
istream& operator>> (istream& inp, Entry &e) {
inp >> e.key;
// get data in two parts to handle white space
string first_word, rest_of_line;
inp >> first_word;
getline(inp, rest_of_line);
e.data = first_word + rest_of_line;
return inp;
}
The input operator should be correct entry.h and entry.cpp were given as part of the assignent.
Line 12: Your constructor declaration with the default value is correct. You should delete line 12.
Line 23: I'll repeat that you don't check if the input operation was successful. If the input file is short, you won't detect it and will put(temp) even if you didn't read anything.