hashtable an array of vectors

Jul 17, 2011 at 5:45am
Hello,
I'm getting an error code saying that push_back is not a function of the vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//from HashTable.h
vector<Entry> table[NUM_BUCKETS]; // an array of vectors<Entry>

//from HashTable.cpp
// Add a key-value pair to hash table
void HashTable::insert(string key, string value)
{
	int hashIndex = hashFunction(key); //compute my position
	
	if(table[hashIndex] == NULL) //insert value if index is NULL
		table[hashIndex].push_back(value);
        else //overwrite the index with new value
}

I just need help on how to use these array of vectors, so that I can figure out the rest of my functions. I believe I'm on the right track.
Last edited on Jul 17, 2011 at 5:51am
Jul 17, 2011 at 5:49am
Your line of code says "pushback" without the underscore. It should be "push_back" with an underscore.

Your vector also store Entry types, but value is a std::string, so unless you have a constructor on Entry that can implicitly construct an Entry from a std::string, you have more work to do.
Jul 17, 2011 at 6:01am
Your line of code says "pushback" without the underscore. It should be "push_back" with an underscore.


Sorry, I must have forgotten the underscore when retyping on the forums. It is there in my actual source code.

Your vector also store Entry types, but value is a std::string, so unless you have a constructor on Entry that can implicitly construct an Entry from a std::string, you have more work to do.


Yes, I do have a constructor.

1
2
3
4
5
class Entry {
public:
    string key;
    string value;
};


Am I using the array of vector correctly in the insert function?
Thank you for your help Jsmith. I hope you can help me figure this out.
Last edited on Jul 17, 2011 at 6:04am
Jul 17, 2011 at 6:41am
I see no constructor in the code you have given.

Also, it looks like you are calling some method push_back of an Entry. I see no function defined with that name.
Jul 17, 2011 at 7:09am
Yes, the constructor is missing.

Also the line if(table[hashIndex] == NULL) doesn't make sense since table[hashIndex] is a vector<Entry>, and vector<T> cannot be compared to NULL.
Jul 17, 2011 at 7:31am
Also, it looks like you are calling some method push_back of an Entry. I see no function defined with that name.


Well I thought I could use a vector like I can an array such as
vector table[hashIndex]= value

But I found out vectors don't work like that. So I tried
table[hashIndex].push_back(value) because vectors have push_back function.

Also the line if(table[hashIndex] == NULL) doesn't make sense since table[hashIndex] is a vector<Entry>, and vector<T> cannot be compared to NULL.


So how would I check if there is any value in hashIndex? Would I do something like
table[hashIndex].empty() to check if that index has anything.

Sorry, I'm learning vectors as I am doing this, and my teacher never taught us vectors, let alone an array of vectors.
I understand what you are saying now as in <Entry> is just a type that I actually have to create.
Jul 17, 2011 at 8:42am
The array subscript operator [] gets a specific element from the vector, which you are then trying to call the push_back() method of. That isn't what you want.

You can do table.push_back(some_Entry) to add entries to the table, or do table[X] = some_Entry to set an entry to a specific location in the vector. You'll need to ensure that said element actually exists, however.
Jul 18, 2011 at 1:28am
You can do table.push_back(some_Entry) to add entries to the table, or do table[X] = some_Entry to set an entry to a specific location in the vector. You'll need to ensure that said element actually exists, however.


Thank you, that is what I needed. I didn't know how exactly to use the vector, but thank you for the explanation!
Topic archived. No new replies allowed.