有人知道这个读取动态整数数组的函数的复杂性是什么,当没有足够的空间容纳下一个输入时,它会将数组大小加倍并将其复制到新的更大数组(内存中的地址)中?
我该如何计算呢?
void readDynamicArray(int* arr, int& physicalSize, int& logicalSize)
{
physicalSize=2;
arr= new int[physicalSize];
int tmpNum;
logicalSize=0;
cin>>tmpNum;
while (tmpNum!=-1)// stop sign is -1, will help know when to stop reading into the array
{
if (logicalSize==physicalSize)
{
arr=newArrLoc(arr, physicalSize, logicalSize);
}
arr[logicalSize]=tmpNum;
logicalSize++;
cin>>tmpNum;
}
}
int* newArrLoc(int* arr, int& physicalSize, int logicalSize)
{
int* newArr=new int[physicalSize*=2];
copyArray(newArr, arr, logicalSize);
delete[] arr;
return newArr;
}
void copyArray (int arr[],int srcArr[] ,int size)
{
int i;
for (i=0;i<size;i++)
{
arr[i]=srcArr[i];
}
}