I need to create an array of linked lists at runtime for a hash table assignment. The sample code that we are given only allows for creating arrays of static size, and I cannot figure out how to modify it to work with a variable, as everything I have tried has resulted in errors.
Sample Code:
typedef std::list<int> INT_LIST;
typedef INT_LIST* INT_LIST_POINTER;
int size = 13;
INT_LIST_POINTER myArray[size];
INT_LIST_POINTER tmpPtr;
// initialize the array to point to empty lists
for (int i=0; i<size; i++){
tmpPtr = new INT_LIST;
myArray[i] = tmpPtr;
}
My Current, nonworking Code:
typedef std::list<int> INT_LIST;
typedef INT_LIST* INT_LIST_POINTER;
INT_LIST_POINTER myArray = new INT_LIST[p];
INT_LIST_POINTER tmpPtr;
for (int i=0; i<n; i++){
INT_LIST* temp = new INT_LIST;
myArray[i] = temp;
}
The major problem seems to be with
myArray[i] = temp;
which says nothing matches those operands.
std::vector.resizeandstd::vector.push_backmethods.const int size = 13;. To demonstrate this, compile using for example GCC with the-pedantic-errorsoption to disable non-standard language extensions.std::listwhen you can just have an array ofstd::list? It's less efficient and less maintainable (note - the deallocation code - another loop fordelete- is missing from the sample).