0

我有一个名为 values 的整数数据数组,我需要在数组中插入一个新的 Integerdata。我曾考虑制作一个临时数组来复制所有内容,然后制作一个大小为原始数组 + 1 的新数组,但我不断收到很多错误。有什么帮助吗?

class IntegerData : public Data {
public:
int value;

// This is the syntax for a constructor that initializes the
// properties value to the parameters
IntegerData(int value) : value(value) {}

}
class ArrayCollection : Collection {
// length of values is always the same as count
Data ** values;
int count;

public:
ArrayCollection() {
  // initialize the array to NULL
  this->values = NULL;
  this->count = 0;
}

~ArrayCollection() {
  // must clean up the internally allocated array here
  if (values != NULL) {
    delete [] values;
  }
}

/**
 * Returns the count of the number of elements in the Collection
 */
int size() const {
  return count;
}



  /**
 * Gets the Data value at the specified index.  If index >= size() then
 * NULL is returned.
 */
Data * get(int index) {
  if (index >= size()) {
    return NULL;
  }
  else {
    return values[index];
  }
}



????-- I need help with this method--
 // I try to dynamically allocate tempArray but I get the error message saying: cannot
 // allocate an object of abstract type 'Data'
void insert(Data * other){
count++;
Data **tempArray = new Data[count];

for(int i = 0; i < count; i++){

tempArray[i] = values[i];
}

delete [] values;



  values = tempArray;

}




}






int main(int argc, const char * argv[]) {
// create an ArrayCollection for our collection of integers
ArrayCollection * collection = new ArrayCollection();

if (argc == 1) {
// they didn't provide any arguments to the program, insert a
// value of zero so that the main function still works
collection->insert(new IntegerData(0));
}
else {
for (int i = 1; i < argc; i++) {
  // read user input for integer value
  int x = atoi(argv[i]);

  // insert it to our collection
  collection->insert(new IntegerData(x));
 }
}

// print the collection
cout << collection->toString() << endl;

// check the implementation of member
IntegerData * five = new IntegerData(5);
cout << "five is a member of collection? " << collection->member(five) << endl;

// now we are going to insert and remove a few items -- MARKER (a)
IntegerData * v0 = (IntegerData *)collection->get(0);
collection->remove(v0);
cout << collection->toString() << endl;

// check after removing the 0th element  -- MARKER (b)
cout << "five is a member of collection? " << collection->member(five) << endl;

collection->insert(v0);
cout << collection->toString() << endl;

// check after inserting the 0th element back
cout << "five is a member of collection? " << collection->member(five) << endl;

// clean up memory
delete five;

// must delete IntegerData instances that we allocated in main
// because they are not deleted by the data structure
for (int i = 0; i < collection->size(); i++) {
delete collection->get(i);
}
// now delete the data structure  -- MARKER (c)
delete collection;
}
4

3 回答 3

2

由于您使用的是 C++,我建议您改用向量:

std::vector<Data *> vect;
...
void insert(Data * other){
    vect.push_back(other);
}

否则在 C 中,该函数的行为可能如下所示:

(假设您首先分配values数组的方式不允许动态调整大小......)

  • 创建一个新的临时values_tmp大小数组(count++)
  • 然后用于memcpy将之前的数据复制到新的数组中
  • 存储值:values_tmp[count] = other;在最后。
  • 释放内存values
  • values用新数组擦除:values = values_tmp;
于 2013-06-22T02:44:14.660 回答
0

从您提供的代码示例中,值数组的初始化并不明显,但是,我会尝试给您一些建议:

如果你必须在这里使用一个数组(并且没有 stl 容器),那么你可能应该将它初始化为一些中等大小的值,比如 128。跟踪这个数字,但也要跟踪其中的项目数(int count )。

当您的数组已满时,您将不得不调整大小。创建一个大小为 256 的新数组(是原始数组的两倍),复制旧数组中的元素,然后删除旧数组。

通过这种方式,您以对数速率调整大小,因此可以摊销插入时间。

(是的,这取自stl向量的作用......如果我们不能使用它,为什么不模仿它呢?)

于 2013-06-22T02:46:07.237 回答
0

这绝对可能不是最有效的方法,但是您可以创建一个具有新增量大小的新数组,循环遍历并逐个复制每个元素,直到到达插入索引,然后复制插入的元素,然后继续复制其余部分并返回新数组。

IntegerData* insert(Data *other, int index)
{
  IntegerData *newArray = new IntegerData[count +1];
  int offset = 0;
  for(int i=0;i<count+1;i++)
  {
    if (i == index)
    {
      newArray[i] = other;
      offset = 1;
    }
    else
    {
      newArray[i] = oldArray[i-offset];
    }
  }
  return newArray;
}
于 2013-06-22T02:52:43.947 回答