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.
也就是说,没有任何东西与这些操作数相匹配。
2 个解决方案
#1
2
If this is C++, why not use std
:
如果这是c++,为什么不使用std:
std::vector<std::list<MyClass> > x;
#2
2
You allocate an array of size p
:
你分配一个大小为p的数组:
INT_LIST_POINTER myArray = new INT_LIST[p];
and then proceed to initialize n
elements:
然后初始化n个元素:
for (int i=0; i<n; i++){
Unless p
and n
are the same thing, this doesn't look right.
除非p和n是一样的,这看起来不对。
P.S. Don't forget to delete[] myArray
when you're done with it.
另外,不要忘记删除[]数组。
#1
2
If this is C++, why not use std
:
如果这是c++,为什么不使用std:
std::vector<std::list<MyClass> > x;
#2
2
You allocate an array of size p
:
你分配一个大小为p的数组:
INT_LIST_POINTER myArray = new INT_LIST[p];
and then proceed to initialize n
elements:
然后初始化n个元素:
for (int i=0; i<n; i++){
Unless p
and n
are the same thing, this doesn't look right.
除非p和n是一样的,这看起来不对。
P.S. Don't forget to delete[] myArray
when you're done with it.
另外,不要忘记删除[]数组。