Deleting an element from an array

nobody somebody at somewhere.com
Tue Feb 3 08:51:12 PST 2009


"Jarrett Billingsley" <jarrett.billingsley at gmail.com> wrote in message 
news:mailman.636.1233678501.22690.digitalmars-d-learn at puremagic.com...
> On Tue, Feb 3, 2009 at 10:54 AM, nobody <somebody at somewhere.com> wrote:
>> Let's see if I understand memmove..
>> The way it's used here, it copies the tail of an array onto that same 
>> array,
>> only starting one index earlier, thus removing the undesired element?
>> Neat.
>
> Right.
>
>> However I just realized that order does not matter in the array I'm 
>> using,
>> so I guess I could use something like:
>> arr[ind] = arr[$-1];
>> arr.length = arr.length -1;
>> Seeing how this only copies once, I'm guessing this is more efficient?
>
> Way more efficient ;)

Would you also happen to know why the following gives an error?

import std.stdio;

void main()
{
 int[3][] arr = [ [0,1,2], [3,4,5], [6,7,8], [9,10,11] ];

 writefln(arr);

 arr[1] = arr[$-1];    // main.d(11): Error: cannot assign to static array 
arr[cast(uint)1]

 //arr[1][0] = arr[$-1][0];    // I could do it manually like this instead, 
or with a loop, but that seems rather silly.
 //arr[1][1] = arr[$-1][1];
 //arr[1][2] = arr[$-1][2];

 arr.length = arr.length - 1;

 writefln(arr);
}




More information about the Digitalmars-d-learn mailing list