0

I need help solving this problem, so if anyone had a similar problem it would help me a lot.

The problem is: How to delete element of array on specific index.

My function:

bool Delete_At_Index(int index)
{
    
    if (index <0 || index>this->numberOfElements)
        return false;
    for (int i = index+1 ; i < this->numberOfElements; i++)
    {
        this->arr[i-1] = this->arr[i];
    }
    this->numberOfElements-- ;
    return true;

}

My program contains an Array class so I use this "this".

For example, for the array 11 8 5 3, if I want to delete the element at position 2, the program prints 11 8 3 3. I don't understand why a duplicate of this 3 appears at the end?

Thanks in advance !

Best regards!

PC Safe
  • 25
  • 5
  • Does anyone can help about this? – PC Safe Oct 27 '22 at 14:49
  • There is a standing library (std) to remove elements for the collection, you never need to write this job by yourself unless just for study purposes. check [this post](https://stackoverflow.com/questions/16013545/how-do-i-erase-elements-from-stl-containers). P.S.1. There is bug in if check the index. P.S.2 "why duplicate?" because pop the last elements normally does not erase the value from memory but just decreases the container's size. Thus, the value is still there, but not in array's range. – Xin Cheng Oct 29 '22 at 04:08

0 Answers0