【问题标题】:insert method for an array of objects c++对象数组的插入方法c ++
【发布时间】:2013-06-22 02:37:04
【问题描述】:

我有一个名为 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;
}

【问题讨论】:

  • 值在哪里被初始化?除非它已经至少是 size ++,否则当您尝试插入时,您将超出数组的范围。
  • 你为什么不让你的新数组大小 1 更大,并让你的插入函数接受 2 个参数。 2 个参数是要插入的数据,以及要插入的索引。然后在函数内部,循环遍历并复制每个值,一旦到达传递值中的索引副本。
  • 我们不是来帮你做作业的。您遇到了什么错误?
  • 对于我的插入方法,我不断收到错误:无法分配抽象类型“数据”的对象。我正在尝试创建一个更大的新数组,并将“值”数组中的所有内容复制到“临时数组”中,然后从值中释放内存并使其指向 tempArray
  • 关于您编辑的答案,我可以告诉您的另一件事是,您对 values 数组边界的运行:for(int i = 0; i &lt; count; i++) 应该是 for(int i = 0; i &lt; count-1; i++),因为您将 count 增加了 1前。对于其余部分,您似乎无法实例化 Data 对象,因为它被声明为抽象,因此 new Data[count]; 应该是 new IntegerData[count];。如消息中所述:无法实例化抽象类,必须对其进行子类化才能使用...继续搜索;)

标签: c++ arrays object memory-management insert


【解决方案1】:

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

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

否则,在 C 语言中,函数的行为可能如下:

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

  • 创建一个大小为 (count++) 的新临时数组 values_tmp
  • 然后使用memcpy将之前的数据复制到新数组中
  • 在末尾存储值:values_tmp[count] = other;
  • values释放内存
  • 用新数组擦除valuesvalues = values_tmp;

【讨论】:

  • 我们必须从头开始使用所有代码,因为这是我第一次使用 c++,我们还没有学习矢量所以我不能使用它们。
  • 为C添加了一个线索:你必须使用一个大小为count+1的临时数组...假设你首先分配values数组的方式不允许动态调整大小...
【解决方案2】:

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

如果您必须在此处使用数组(并且没有 stl 容器),那么您可能应该将其初始化为某个大小适中的值,例如 128。跟踪这个数字,但也要跟踪其中的项目数(int count)。

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

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

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

【讨论】:

  • 这是个好主意,但这个项目的主要重点是内存管理。每次我想插入一个新的 IntegerData 时,她都希望我动态地将一个数组分配给一个新的大小,而我不知道该怎么做。
  • @user2510809 好的,您熟悉“新”运算符吗?这是用来动态分配内存的。
  • 是的,但是当我在插入方法中使用它时,它不会编译。我有 Java 经验,但这是我第一次使用 c++
【解决方案3】:

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

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2018-06-06
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多