我有一个名为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;
}
答案 0 :(得分:2)
由于您使用的是C ++,我建议使用向量:
std::vector<Data *> vect;
...
void insert(Data * other){
vect.push_back(other);
}
否则在C中,函数可能表现如下:
(假设您首先分配values
数组的方式不允许动态调整大小...)
values_tmp
memcpy
将以前的数据复制到新数组values_tmp[count] = other;
存储在最后。values
values
:values = values_tmp;
答案 1 :(得分:0)
从您提供的代码示例中,您的values数组的初始化并不明显,但是,我会尝试给您一些建议:
如果 在这里使用数组(并且没有stl容器),那么你应该把它初始化为一些中等大小的值,比如128.跟踪这个数字,但也要跟踪其中的项目数(int count)。
当你的阵列变满时你将不得不调整大小。创建一个大小为256的新数组(其大小是原始数据的两倍),复制旧数组中的元素,然后删除旧数组。
通过这种方式,您将以对数速率调整大小,因此插入时间可以摊销。
(是的,这取自stl矢量的作用......如果我们不能使用它,为什么不模仿它?)
答案 2 :(得分: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;
}