Deleting an element from an array
Jarrett Billingsley
jarrett.billingsley at gmail.com
Tue Feb 3 07:34:52 PST 2009
On Tue, Feb 3, 2009 at 10:14 AM, Frank Benoit
<keinfarbton at googlemail.com> wrote:
>
> arr = arr[ 0 .. lowerBound ] ~ arr[ upperBound .. $ ];
>
That's simple enough, but inefficient.
Something like this:
import std.c.string; // or import tango.stdc.string;
T[] erase(T)(ref T[] arr, size_t idx)
{
if(arr.length == 0)
throw new Exception("FAILCOPTER");
if(idx < arr.length - 1)
memmove(&arr[idx], &arr[idx + 1], T.sizeof * (arr.length - idx - 1));
arr.length = arr.length - 1;
return arr;
}
More information about the Digitalmars-d-learn
mailing list