D idom for removing array elements
albert-j via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 30 00:50:14 PST 2017
On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote:
> Removing works by overwriting the array with only the wanted
> values and discarding the rest.
But then why do I get this:
import std.stdio, std.algorithm, std.array;
int[] arr;
foreach (i; 0..10) arr ~= i; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
writeln("&arr[0]:", &arr[0]); // prints "7F56FAE93000"
arr = arr.remove!(x => x > 5).array;
writeln("&arr[0]:", &arr[0]); // prints "7F56FAE92020"
writeln("arr:",arr); // prints: "[0, 1, 2, 3, 4, 5]"
It looks like arr.remove allocates new memory and copies the data
there.
More information about the Digitalmars-d-learn
mailing list