假设您有一个容量为5的数组,并且假设您还有一个可变计数器来计算添加到数组中的每个条目。你将如何实现数组的浮点操作?使用C++语法?数组重新分配C++
void BST::reallocate()
{
item *new_array = new item[size*2];
for (int array_index = 0; array_index < size * 2; array_index++)
{
if (! items[array_index].empty)
{
new_array[array_index].theData = items[array_index].theData;
new_array[array_index].empty = false;
}
}
maxSize += size;
delete [] items;
items = NULL;
items = new_array;
}
如何重新分配数组? BST ctor位于私人物品结构下方,以消除任何混淆。
BST::BST(int capacity) : items(new item[capacity]), Position(0),
leftChild(0), rightChild(0), maxSize(capacity)
{
}
这是在BST头:
private:
int size;
int maxSize;
int Position;
int leftChild;
int rightChild;
struct item
{
bool empty;
data theData;
};
item *items;
的quesetion是,我似乎具有硬时间与我的项目阵列的再分配。
问题是它正在崩溃 – user40120 2009-11-19 20:02:00
它在哪里崩溃? – 2009-11-19 20:05:18
其在从项目[i] .theData到new_array [i] .Data的赋值时崩溃。 – user40120 2009-11-19 20:07:44