Are there an equivalent to C#'s List in D's stdlib?

Mike Parker aldacron at gmail.com
Fri Jan 8 03:58:15 UTC 2021


On Friday, 8 January 2021 at 03:22:49 UTC, Jack wrote:

> tedius, imo. Now, if I went to use filter, could I remove that 
> item and somewhat get an array back, without perform a new 
> array allocation? currently i'm with this, that as far i know, 
> does perform memory allocation by array() call:
>
> arr = arr.filter!(x => x != value).array;

See std.algorithm.mutation.remove. One overload takes an offset, 
but the other takes a predicate. Example from the docs:

```
static immutable base = [1, 2, 3, 2, 4, 2, 5, 2];

int[] arr = base[].dup;

// using a string-based predicate
writeln(remove!("a == 2")(arr)); // [1, 3, 4, 5]

// The original array contents have been modified,
// so we need to reset it to its original state.
// The length is unmodified however.
arr[] = base[];

// using a lambda predicate
writeln(remove!(a => a == 2)(arr)); // [1, 3, 4, 5]
```




More information about the Digitalmars-d-learn mailing list