0

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.

user1940516
  • 41
  • 1
  • 3
  • 6
  • 1
    Very unfortunately, you're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier), as well as a using directive in a header. – chris Mar 22 '14 at 03:47

3 Answers3

1

You shouldn't be checking for NULL because you have an array of std::list objects, not pointers.

You can check if the list at index i has any elements with:

if (!hashTable[i].empty())
{
    // there are elements in the list
}

Or

if (hashTable[i].size())
{
    // there are elements in the list
}

Also in your print() function, you're using an iterator of type std::list<int>, but it should be std::list<string>, which matches the declaration of hashTable.

Eugene S
  • 3,092
  • 18
  • 34
0

Your problem is not with std list, it is with the array.

You could try using the std::array

#include <array>

std::array< std::list<string>, 100> hashTable;

this way you can check if it is emtpy

if (!hashTable.empty())
{
  // do stuff
}

and inside you can check each list

if(!hashTable[i].empty())
{
  // do even more stuff
}
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
0

Awesome, Thanks a lot guys! I understand how to check each array index and iterate through the list! Heres what I did:

1 #include "hash2.h"
2
3 #include<iostream>
4 #include<string>
5 #include<fstream>
6
7 using namespace std;
8
9 void Hash::processFile(string fileName)
10 {
11  string word;
12  ifstream myIfile(fileName.c_str());
13  while(getline(myIfile, word))
14  {
15   int index = hf(word);
16   hashTable[index].push_back(word);
17  }
18  myIfile.close();
19 }
20
21 void Hash::print()
22 {
23  for(int i = 0; i < 100; i++)
24  {
25   if(hashTable[i].empty() != true)
26   {
27    list<string>::iterator r;
28    for(r = hashTable[i].begin(); r != hashTable[i].end(); ++r)
29    {
30     cout << *r << endl;
31    }
32   }
33  }
34 }
35
36 int Hash::hf(string ins)
37 {
38  return ( (int) ins[0] ) % 100;
39 }
user1940516
  • 41
  • 1
  • 3
  • 6