Search code examples
arraysvectord

Vectors and dynamic arrays in D


I was thinking that dynamic arrays were a replacement for vectors in D, but it seems they have no remove function (only associative arrays do) which is rather a limitation for a vector so I'm wondering if I've got that right. If a have an array like follows,

uint[] a;
a.length = 3;
a[0] = 1;
a[1] = 2;
a[2] = 3;

Then the only way I've found to remove, say, the second element is,

a = a[0..1] ~ a[2];

But that doesn't seem right (but maybe only because I don't understand this all yet). So is there a vector and is there another way of removing an element from a dynamic array?

Thanks.


Solution

  • You could use std.algorithm.remove(), which works not only with arrays but with generic ranges. Example:

    import std.algorithm;
    
    void main() {
        uint[] a = [1, 2, 3];
        a = a.remove(1);
        assert(a == [1, 3]);
    }