D idom for removing array elements
albert-j via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 30 04:31:33 PST 2017
On Monday, 30 January 2017 at 10:45:03 UTC, cym13 wrote:
> Meh.
>
> Forget that, bad memory. remove isn't working in-place. However
> slapping ".array" is still asking explicitely for reallocation,
> so just forget it. Here is a code that works:
>
> import std.conv;
> import std.stdio;
> import std.format;
> import std.algorithm;
>
> void main(string[] args) {
> int [] arr = [8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
>
> auto before = arr.ptr;
> arr = arr.remove!(x => x>5);
> auto after = arr.ptr;
>
> assert(arr == [0, 1, 2, 3, 4, 5], arr.to!string);
> assert(before == after, "%s %s".format(before, after));
> }
OK, got it. Can you do removal without reallocation with
std.container.array?
Array!int arr;
foreach (i; 0..10) arr ~= i;
More information about the Digitalmars-d-learn
mailing list