【发布时间】:2018-11-26 20:51:35
【问题描述】:
我真的不确定为什么这不起作用。就像数组大小没有加倍一样。我确定我缺少一些简单的东西,但我无法弄清楚为什么它不能正常工作。
void add_element(int* &array, int &size , int &count)
{
int tempp;
cout << "What number do you want to add ? " << endl;
cin >> tempp;
int temp = size * 2;
int *newArr;
newArr = new int[temp];
if(count == size)
{
for (int i = 0; i < size; i++)
{
newArr[i] = array[i];
}
size = temp;
delete[] array;
++count;
newArr[count] = tempp;
array = newArr;
}
}
【问题讨论】:
-
就像数组大小没有加倍 -- 你怎么知道数组大小没有加倍?请发帖minimal reproducible example。
-
您观察到什么行为?你期望什么? “我认为它不起作用”不是一个好的问题描述。
-
if (count == size) ... 不应该是 if (count