Я думаю, что ключ в том, что вам не нужно копировать пустые ячейки.
void insertrange(T item[], int sizerange, int index)
{
// This actually points past the end of the current array, right?
int onePastLastEntry = this->size;
// Still need to make sure the new array is large enough
if(this->size + sizerange >= this->capacity) //if the size + added size are bigger than the capacity
{
this->enlarge(this->capacity + sizerange); //enlarge the array
}
// you should be able to go forward instead of backwards
for(i = index; i < onePastLastEntry ; i++)
{
// move the current element
this->array[i + sizerange] = a[i];
// then copy the new value
this->array[i] = item[i - index];
}
Вы могли бы сделать цикл с нуля, перейдя также к onePastLastEntry - index
, но это делает математику странной:
// you should be able to go forward instead of backwards
for(i = 0; i < onePastLastEntry - index; i++)
{
// now you have to add the index in two places here
this->array[i + index + sizerange] = a[i + index];
// and add the index in the copy as well
this->array[i + index] = item[i];
}