I don't like slices in D
qznc
qznc at web.de
Thu Oct 17 15:36:03 PDT 2013
On Thursday, 17 October 2013 at 18:41:53 UTC, Vitali wrote:
> The use case is:
>
> void removeElement(ref int[] arr, int index) {
> arr = arr[0..index] ~ arr[index+1..$];
> }
The meaning of this code is: Create a new array out of composing
two slices and assign it to arr. Of course, there is allocation
happening.
What you probably want is: Move arr elements index+1..$ to
index..$-1 and decrease length by one, preserving capacity. You
can use std.algorithm.remove, which does move part. Hence:
import std.algorithm: remove;
void removeElement(ref int[] arr, int index) {
remove(arr,index);
arr.length--;
}
More information about the Digitalmars-d
mailing list