I am struggling with the syntax needed to declare, create, and manipulate a pointer to an array of list objects. I’m creating a hash table template class for a course assignment, using chaining to deal with has collisions.
The way my hash table is supposed to be is an array of list objects. std::list is the only standard library data class I’m allowed to use for the assignment, so vector is out.
My questions are:
How do I declare a pointer to an array of std::list? I have:
private:
std::list<T> * table;
How do I create the array? I have:
table = new std::list<T>[3]; // start with table size 3
I think I have the declaration and construction right, but I’m not 100% sure. Lastly, how do I interact with the list’s? I’ve got:
for (int i = 0; i < _tableSize; i++) { // _tableSize is the array's size
if (!table[0][i].empty()) {
table[0][i].push_back(thing);
}
}
Somehow table[0][i] seems wrong to access a cell of the array that table points to. What should it look like?
Since
tableis astd::list*,table[i][j]means(table + i)->operator[](j); that is, it calls a member function (with argumentj) on thestd::listobject starting at addresstable + i.It follows that, since
std::listdoesn’t overload[],table[0][i].empty()is wrong (doubly wrong ifTinstd::list<T>is such that it hasn’t a member functionempty).Shortly, I think that what you need is a simple
table[list_index_on_array].empty()to check for emptiness.Everything else looks ok.