Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 19, 2010 remove element(s) from dyn array | ||||
---|---|---|---|---|
| ||||
Hello, * What is the D-idiomatic way to remove an element from a dynamic array? * Ditto, for a slice, but with the 2 variants expression vs statement: a.removeSlice(i1, i2); a2 = a1.removeSlice(i1, i2); * Can I rely on a.length = a.length - 1 to just remove the last element? If yes, is this considered good practice? Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com |
November 19, 2010 Re: remove element(s) from dyn array | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | On 19.11.2010 16:11, spir wrote: > Hello, > > > * What is the D-idiomatic way to remove an element from a dynamic array? > * Ditto, for a slice, but with the 2 variants expression vs statement: > a.removeSlice(i1, i2); That would most likely be: std.array; int[] a = ...; replace(a,i1,i2,null); > a2 = a1.removeSlice(i1, i2); hm, not sure how idiomatic is this: a2 = a1.dup; replace(a2,i1,i2,null); > * Can I rely on > a.length = a.length - 1 > to just remove the last element? If yes, is this considered good practice? For this one I'd use: a = a[0..$-1]; this way to me it's more clear how array is changed. > > Denis > -- -- -- -- -- -- -- > vit esse estrany ☣ > > spir.wikidot.com > -- Dmitry Olshansky |
November 19, 2010 Re: remove element(s) from dyn array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On 19.11.2010 17:53, Dmitry Olshansky wrote: > On 19.11.2010 16:11, spir wrote: >> Hello, >> >> >> * What is the D-idiomatic way to remove an element from a dynamic array? >> * Ditto, for a slice, but with the 2 variants expression vs statement: >> a.removeSlice(i1, i2); > That would most likely be: > std.array; Ehm, that should have been import std.array. [snip] -- Dmitry Olshansky |
November 19, 2010 Re: remove element(s) from dyn array | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir:
> * Can I rely on
> a.length = a.length - 1
> to just remove the last element? If yes, is this considered good practice?
I think it's idiomatic in D, so it's OK.
Currently you may also write that as:
a.length -= 1;
But this is not supported yet:
a.length--;
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation