我需要在运行时为哈希表分配创建一个链表的数组。我们给出的示例代码只允许创建静态大小的数组,我无法弄清楚如何修改它以使用变量,因为我尝试的所有内容都会导致错误。
示例代码:
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;
}
主要问题似乎是
myArray[i] = temp;
表示什么都不匹配那些操作数。
答案 0 :(得分:2)
如果这是C ++,为什么不使用std
:
std::vector<std::list<MyClass> > x;
答案 1 :(得分:2)
您分配了一个大小为p
的数组:
INT_LIST_POINTER myArray = new INT_LIST[p];
然后继续初始化n
元素:
for (int i=0; i<n; i++){
除非p
和n
相同,否则看起来并不合适。
P.S。
}当你完成它时,别忘了delete[] myArray
。