I am trying to represent a hash table as a vector of pair < string, int>. I am using a hash function to return the value of the index of the vector where I wish to place the pair. I have been able to successfully create a pair and index the pair's string with the hash function. Now that I know where I want to place my pair in my vector I try to put it there but my program has a segmentation fault at this point. My hash function:
size_t hashfunction(const string& ident){
unsigned hash = 0;
for(int i = 0; i < ident.size(); ++i) {
char c = ident[i];
hash ^= c + 0x9e3779b9 + (hash<<6) + (hash>>2);
}
return hash;
}
My main function:
int main(){
vector < pair < string, int > > hashtable;
pair <string, int> testone ("bartering", 5);
size_t testoneindex = hashfunction(testone.first);
hashtable[testoneindex] = testone;
return 0;
}
This section of code compiles but produces a segmentation fault at the line
hashtable[testoneindex] = testone;
What am I doing wrong?
vector. For example, initialize yourvectorto have 1000 buckets, and usehashfunction(..) % 1000. That brings the next question: how do you plan to handle hash collisions?