"Frank Benoit" wrote > nobody schrieb: >> "Denis Koroskin" <2kor...@gmail.com> wrote in message >> news:op.uor1gzqho7c...@korden-pc... >>> On Tue, 03 Feb 2009 15:46:52 +0300, nobody <someb...@somewhere.com> >>> wrote: >>> >>>> What is the best way to completely remove an element from an array? >>>> >>>> For example you have an array: >>>> [1,2,3,4,5,6] >>>> and want to remove element "3" in such a way that the resulting array >>>> is: >>>> [1,2,4,5,6] >>>> >>>> Thanks. >>>> >>>> >>> import std.array; >>> >>> auto arr = [0, 1, 2, 3, 4, 5]; >>> >>> int lowerBound = 2; >>> int upperBound = 4; >>> >>> // erases elements [lowerBound, upperBound), >>> // total of upperBound - lowerBound elements >>> arr.erase(lowerBound, upperBound); >>> >>> assert(arr == [0, 1, 4, 5]); >>> >> >> Ah I'm sorry, I forgot to mention I'm using D1, not D2. >> >> Thanks for the reply though :) >> >> > > arr = arr[ 0 .. lowerBound ] ~ arr[ upperBound .. $ ];
Or, if you want to avoid heap activity, use memmove: memmove(arr.ptr + lowerBound, arr.ptr + upperBound, sizeof(*arr.ptr) * (arr.length - upperBound)); arr.length = arr.length - (upperBound - lowerBound); Unfortunately, doing a slice assign doesn't work on overlapping regions (shouldn't there be a lib function for this?), otherwise, the following would be braindead simple: arr[lowerBound..$-(upperBound-lowerBound)] = arr[upperBound..$]; arr.length = arr.length - (upperBound - lowerBound); -Steve