2013-06-22 47 views
0

我有一個稱爲值的整型數據數組,我需要在數組中插入一個新的Integerdata。我曾考慮過使用臨時數組來複制所有內容,然後創建一個大小爲+ 1的原始數組,但我不斷收到很多錯誤。任何幫助?對象數組的插入方法C++

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

值在哪裏初始化?除非它的大小已經至少爲++,否則當你嘗試插入時,你會超出數組範圍。 – AndyG

+0

爲什麼不讓你的新數組大小更大,並且你的插入函數需要2個參數。這兩個參數是要插入的數據,以及要插入的索引。然後在函數內部循環並複製每個值,並且一旦到達傳入值的索引副本。 – krb686

+0

我們不是來做你的功課。你得到的錯誤是什麼? – Adrian

回答

2

由於您使用C++我會建議使用矢量來代替:

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

否則,在C,該功能可這樣的表現:

(假設你的方式分配values陣列首先不允許動態調整大小......)

  • 創建一個新的te大小(計數++)的mporary陣列values_tmp
  • 然後使用memcpy到先前數據複製到新的數組
  • 存儲的值:values_tmp[count] = other;末。
  • 釋放內存爲values
  • 擦除values新的數組:values = values_tmp;
+0

我們必須從頭開始使用所有的代碼,因爲這是我第一次使用C++,所以我們沒有' t學習過的矢量,所以我不被允許使用它們。 – user2510809

+0

爲C添加了引導:您必須使用一個大小爲count + 1的臨時數組......假設您首先分配'values'數組的方式不允許動態調整大小... –

0

你的價值觀數組的初始化是不是從你提供的代碼示例顯而易見的,但是,我會盡力給你一些建議:

如果使用此數組(沒有STL容器),那麼你或許應該把它初始化爲一些中等規模的價值,說這個數字的128跟蹤,而且跟蹤的其中的項目數量(int coun T)。

當你的數組變滿時,你將不得不調整大小。創建一個大小爲256(這是原始大小的兩倍)的新數組,複製舊數組中的元素,然後刪除舊數組。

通過這種方式,您可以以對數速率調整大小,因此插入時間可以攤銷。

(是的,這是從什麼STL的vector沒有......如果我們不能使用它,爲什麼不模仿什麼時間?)

+0

這是一個好主意,但這個項目的主要重點是內存管理。她希望我每次想插入一個新的IntegerData時都會動態地將一個數組分配給一個新的大小,我不知道該怎麼做。 – user2510809

+0

@ user2510809好的,你是否熟悉'新'操作符?這是用來動態分配內存的。 – AndyG

+0

是的,但是當我在插入方法中使用它時,它不會編譯。我對Java有經驗,但這是我第一次使用C++ – user2510809

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