removing an item from a dynamic array
    Steven Schveighoffer 
    schveiguy at yahoo.com
       
    Wed Oct 26 04:35:02 PDT 2011
    
    
  
On Tue, 25 Oct 2011 05:51:25 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:
> maarten van damme:
>
>> import std.algorithm;
>> struct Loc {
>> uint row;
>> uint col;
>> }
>> void main(){
>> Loc[] testArray;
>> Loc a={3,2};
>> Loc b={5,3};
>> testArray~=a;
>> testArray~=b;
>> remove(testArray,a);
>> }
>> gives the same error
>
> The second argument of remove() needs to be an index, a size_t.
>
> This works:
>
> import std.stdio, std.algorithm;
> struct Loc {
>     uint row, col;
> }
> void main() {
>     auto a = Loc(3, 2),
>          b = Loc(5, 3);
>     auto data = [a, b];
>     writeln(remove(data, 0));
>     writeln(data);
> }
>
>
> It prints:
> [Loc(5, 3)]
> [Loc(5, 3), Loc(5, 3)]
>
> So curiously remove() doesn't work in-place, I think this is a bug or a  
> design bug.
 From the documentation:
"The original array has remained of the same length because all functions  
in std.algorithm only change content, not topology."
In other words, remove just moves unwanted elements to the back of the  
array, then returns the front of it.
It looks like it works as designed.
-Steve
    
    
More information about the Digitalmars-d-learn
mailing list