My code is below. I'm pretty sure my issues are spanning my my .resize(). It's possible I'm doing my hashing completely wrong too, but it was right at one point. This code wont run at all right now, but it compiles. Is there a better way to go about sizing my container?
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <list>
using namespace std;
typedef vector<list<string> > hash_set;
void hashInsert(hash_set &vtr, string word);
int getHash(string word);
int main()
{
int size = 0;
string word;
hash_set vtr;
ifstream firstfile ("doc1.txt");
if (firstfile)
{
while ( firstfile >> word )
{
size++;
vtr.resize(2*size);
// hashInsert(vtr, word);
}
firstfile.close();
}
else cout << "Unable to open file";
ifstream samefile ("doc1.txt");
if (samefile)
{
while ( samefile >> word )
{
hashInsert(vtr, word);
}
samefile.close();
}
else cout << "Unable to open file";
for (size_t vtrIndex=0; vtrIndex < vtr.size(); ++vtrIndex)
{
for (list<string>::iterator it = vtr[vtrIndex].begin(); it != vtr[vtrIndex].end(); ++it)
{
cout << *it << " " << vtrIndex << '\n';
}
}
return 0;
}
void hashInsert(hash_set &vtr, string word)
{
vtr[getHash(word)].push_back(word);
}
int getHash(string word)
{
int output;
int i;
output = word[word.size()-1];
for (i = (word.size()-2); i >= 0; i--)
{
output = (128*output + word[i]) % 127;
}
return output;
}