假设你有一个容量为5的数组,并且假设你有一个计数变量,它计算添加到数组中的每个条目.你怎么会真正的数组呢?使用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;
}
Run Code Online (Sandbox Code Playgroud)
你如何重新分配数组?BST ctor下面是私有项结构,只是为了消除任何混淆.
BST::BST(int capacity) : items(new item[capacity]), Position(0),
leftChild(0), rightChild(0), maxSize(capacity)
{
}
Run Code Online (Sandbox Code Playgroud)
这是在BST标题中:
private:
int size;
int maxSize;
int Position;
int leftChild;
int rightChild;
struct item
{
bool empty;
data theData;
};
item *items;
Run Code Online (Sandbox Code Playgroud)
问题是,我似乎很难重新分配我的items数组.
| 归档时间: |
|
| 查看次数: |
1386 次 |
| 最近记录: |