Runtime error in hashtable implementation
Apr 25, 2013 at 4:01am Apr 25, 2013 at 4:01am UTC
I am getting a strange runtime error when trying to run my hash table program. I have it separated in 3 separate files. I believe I have narrowed down where the error is occurring. It is either happening in
insert(const &string s)
or in the implementation of
vector<list<string>> ht
. I would appreciate some input. This is the implementation of insert():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void HTable::insert(const string &s)
{
int h = hash(s);
cout<<h<<endl;
list<string> &tempList = ht[h];
if ( find( tempList.begin( ), tempList.end( ), s ) == tempList.end( ) )
{
tempList.push_back( s );
currentSize++;
}
cout<<currentSize<<endl;
}
and this is the declaration and implementation of my vector:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#ifndef HTABLE_H
#define HTABLE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <cassert>
#include <algorithm>
using namespace std;
//template <class T>
class HTable
{
public :
HTable();
HTable(const int &);
~HTable();
bool contains(const string &);
int hash(const string &);
void insert(const string &);
void remove(string &);
void print();
private :
vector<list<string>> ht;
int TABLESIZE;
int key;
int currentSize;
};
inline HTable::HTable()
{
TABLESIZE=33;
vector<list<string>> ht (TABLESIZE);
currentSize = 0;
}
//template <class T>
inline HTable::HTable(const int &size)
{
TABLESIZE = size;
vector<list<string>> ht (TABLESIZE);
currentSize = 0;
}
I want to mention that I have tried creating a pointer for the vector such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#ifndef HTABLE_H
#define HTABLE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <cassert>
#include <algorithm>
using namespace std;
//template <class T>
class HTable
{
public :
HTable();
HTable(const int &);
~HTable();
bool contains(const string &);
int hash(const string &);
void insert(const string &);
void remove(string &);
void print();
private :
vector<list<string>> *ht;
int TABLESIZE;
int key;
int currentSize;
};
inline HTable::HTable()
{
TABLESIZE=33;
ht = new vector<list<string>>(TABLESIZE);
currentSize = 0;
}
//template <class T>
inline HTable::HTable(const int &size)
{
TABLESIZE = size;
ht = new vector<list<string>>(TABLESIZE);
currentSize = 0;
}
and it is giving me some sort of compilation error saying I cannot convert a type string to type list.
Apr 25, 2013 at 5:29am Apr 25, 2013 at 5:29am UTC
Apr 26, 2013 at 1:00am Apr 26, 2013 at 1:00am UTC
Did not know which one was the best to post on. Never will happen again sir.
Topic archived. No new replies allowed.