removing an item from a dynamic array
Steven Schveighoffer
schveiguy at yahoo.com
Wed Oct 26 04:43:26 PDT 2011
On Tue, 25 Oct 2011 06:13:09 -0400, simendsjo <simendsjo at gmail.com> wrote:
> On 25.10.2011 11:51, bearophile 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.
>>
>> Bye,
>> bearophile
>
> Yes, probably a bug. This example from the documentation fails:
> import std.stdio, std.algorithm;
> void main() {
> int[] a = [ 0, 1, 2, 3 ];
> assert(remove!(SwapStrategy.unstable)(a, 1) == [ 0, 3, 2 ]);
> }
Most certainly this is a bug. Here is the resulting array and final state
of a:
import std.stdio, std.algorithm;
void main() {
int[] a = [ 0, 1, 2, 3 ];
writeln( remove!(SwapStrategy.unstable)(a, 1));
writeln(a);
}
output:
[3, 1, 2]
[3, 1, 2, 3]
Clearly, index 0 was removed, not index 1. Please file a bug.
-Steve
More information about the Digitalmars-d-learn
mailing list