I'm having some trouble printing an array of lists (ex. list<string> hashTable[100]
)
Heres what I have so far, my question is at the bottom:
hash2.h
1 #ifndef __HASH2_H
2 #define __HASH2_H
3
4 #include<string>
5 #include<list>
6 #include<fstream>
7
8 using namespace std;
9
10 class Hash
11 {
12 public:
13 void processFile(string fileName);
14 void print();
15
16 private:
17 list<string> hashTable[100];
18
19 private:
20 int hf(string ins);
21
22
23 };
24
25 #endif
hash_function2.cpp
1 #include<iostream>
2 #include<string>
3 #include<fstream>
4 #include "hash2.h"
5
6 using namespace std;
7
8 void Hash::processFile(string fileName)
9 {
10 string word;
11 ifstream myIfile(fileName.c_str());
12 while(getline(myIfile, word))
13 {
14 int index = hf(word);
15 hashTable[index].push_back(word);
16 }
17 myIfile.close();
18 }
19
20 void Hash::print()
21 {
22 for(int i = 0; i < 100; i++)
23 {
24 if(hashTable[i] != NULL)
25 {
26 list<int>::iterator i;
27 for(i = hashTable[i].begin(); i != hashTable[i].end(); ++i)
28 {
29 cout << *i << endl;
30 }
31 }
32 }
33 }
34
35 int Hash::hf(string ins)
36 {
37 return ( (int) ins[0] ) % 100;
38 }
main2.cpp
1 #include "hash2.h"
2 #include<iostream>
3 #include<iomanip>
4 #include<fstream>
5
6 using namespace std;
7
8 int main()
9 {
10 Hash hashTable;
11 hashTable.processFile("test1.txt");
12 hashTable.print();
13 }
So, what I have now is the processFile function, which takes the text file, reads each word in, performs the hash function(crappy, i know), then puts that word in the array index which the hash function returned. That is working fine I think.
What im having issues with is essentially using the STL list. So, in hash_function2.cpp, i want to print my hash table. I'm not sure how to check if the array index is empty(doesn't have any of the words I stored), and I am also not sure how to print all the strings in the list at a given array index. Basically what I would like to do is just print my hash table; get my print() function working.
If anyone could point me in the right direction that would be awesome! It would be greatly appreciated.