On Friday, 18 October 2013 at 13:38:01 UTC, Vitali wrote:
My prevoius example of the function "removeElement(ref int[], int)" will not work. The right implementation would be:
void removeElement(ref int[] arr, int index) {
  arr[index..$-1] = arr[index+1..$].dup;

.dup allocates a copy. The right implementation would be to copy the data yourself, then reduce length, and then just leave it at that.

void removeElement(ref int[] arr, int index) {
    for(size_t idx = index; idx < arr.length - 1; idx++)
        arr[idx] = arr[idx + 1];
arr.length = arr.length - 1; // arr.length-- won't compile due to silliness
}

Reply via email to