我正在處理一個程序,用戶不斷輸入數字,當數組已滿時,它將保存到數組中,我試圖將原始數組複製到一個新數組中,並繼續填充該數組,但我不能讓它工作。這裏是我的代碼到目前爲止試圖動態增加數組的大小
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int size;
cout << "Please enter how many numbers you want to enter: ";
cin >> size;
double *array = new double*[size];
cout << "Please enter your numbers: ";
for(int i = 0; i < size; i++) {
cin >> array[i];
if(i == size-1) {
int newSize = 2*size;
double *arrayb = new double*[newSize];
for(int i = 0;i<size;i++) {
arrayb[i] = array[i];
}
delete [] array;
array = arrayb;
size = newSize;
}
}
}
改爲使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 – TartanLlama
表達式'new double * [size]'將'size' *指針*數組分配給'double'。在32位系統上這會導致麻煩。 –
請詳細說明您的問題? *你怎麼能不「讓它工作」?請[閱讀關於如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –