I'm new to C++ and this is my first time dealing with hash tables. I am doing this for a homework assignment. I believe that my insert function is not working properly. Here is my class HTable, and insert() function
also this is the sample output in which my teacher provided
size of the Hash Table? 33
HTable::HTable( 33 )
read strings from a file
name of file to read from? strings.txt
read commands from a file
name of file to read from? cmds.txt
0: the
1:
2:
3:
4:
5: is test
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27: paragraph
28: in
29: beginning
30: second
31: a
32:
a xyz
0: the
1:
2: xyz
3:
4:
5: is test
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27: paragraph
28: in
29: beginning
30: second
31: a
32:
unknown command: 'x'
d test
0: the
1:
2: xyz
3:
4:
5: is
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27: paragraph
28: in
29: beginning
30: second
31: a
32:
a test
0: the
1:
2: xyz
3:
4:
5: is test
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27: paragraph
28: in
29: beginning
30: second
31: a
32:
d paragraph
0: the
1:
2: xyz
3:
4:
5: is test
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27:
28: in
29: beginning
30: second
31: a
32:
d second
0: the
1:
2: xyz
3:
4:
5: is test
6:
7:
8: number
9:
10:
11: This fair
12:
13: strings
14:
15:
16:
17:
18: of
19: appear
20:
21:
22:
23: that
24:
25:
26:
27:
28: in
29: beginning
30:
31: a
32:
Press any key to continue . . .
Also, this is the output I am receiving.
size of the Hash Table? 33
read strings from a file
name of file to read from? strings.txt
read commands from a file
name of file to read from? cmds.txt
a xyz
unknown command: 'x'
d test
a test
d paragraph
d second
Press any key to continue . . .
as you can see, mine is completely off from my teachers output. I believe it's involving my insert(), or maybe could be my print function which I am having trouble coding. cannot seem to figure out how to code the print function.
you are not close to implement the insert function, to what I understand should be your hash table specification.
1. vector<list<string> > ::iterator it;
it- is now iterator on the vector type, not on the list type.
it is pointless to compare it with the list iterators which returned when you do List.begin() or List.end().
2. I understand you should have vector of lists of strings. did you try to iterate on the vector, or on the list? because you should doing it on both, to find the correct place to insert the new element.
3. (*it).begin() may be legal, but I think it is not what you wanted to do.
I can see it visually on what I am supposed to do, but I cannot seem to translate it into coding. This is what I can see on what to do. first push strings into the list, then push back theses lists into vector of lists. But cannot seem to translate this into code
I'm not sure how to really code that. I'm still very new to C++. My knowledge is very limited.
Write down what you need to do. Write code to accomplish it.
To insert a unique key into a hash table I need to:
hash the key to determine which bucket the key would be stored in.
search the bucket to see if the key is already in the bucket.
if it's not in the bucket, store it in the bucket.
Hashing the key will involve one of the three member functions with 'hash' in the name that take a string as an argument. (You might also want to pare down the number of functions. You only need one: unsigned hash(const std::string& key) const. I use unsigned because it doesn't seem likely you'll want it to return a negative number with which to index the vector.)
By the way, you should be using the hash function to determine the bucket to search in your previously posted remove function as well.
to be honest with you, I am not sure how to translate what you are saying into code.
hash the key to determine which bucket the key would be stored in.
search the bucket to see if the key is already in the bucket.
if it's not in the bucket, store it in the bucket.
It's pretty straightforward.
1 2 3 4 5 6 7 8 9 10 11
void HTable::insert( const string &s )
{
// hash the key to determine which bucket the key would be stored in.
int bucketNum = hash(s) ;
list<string>& bucket = List[bucketNum] ;
// search the bucket to see if the key is already in the bucket.
if ( bucket.empty() || bucket.end() == find(bucket.begin(), bucket.end(), s) )
bucket.push_back(s) ; // if it's not in the bucket, store it in the bucket.
}
ok, awesome. Thanks. When I test this out, and call my hash(), i am receiving an error saying that my vector subscript it is out of range. is my hash() correct?