Pop an element from an array inplace?

Simen kjaeraas simen.kjaras at gmail.com
Tue Sep 7 06:59:40 PDT 2010


Paolo Invernizzi <paolo.invernizzi at srlabs.it> wrote:

> Hi all,
>
> What's the best way to find an element into an array, drop it and shrink  
> the array inplace, in D2?
>
> Thanks in advance, Paolo

T extract( T )( ref T[] haystack, const T element ) {
     auto loc = indexOf( haystack, element );
     T result = haystack[loc];
     haystack = haystack[0..loc] ~ haystack[loc+1..$];
     return result;
}

Not tested, but should work.

-- 
Simen


More information about the Digitalmars-d-learn mailing list