Vectors and dynamic arrays in D

I thought dynamic arrays replace vectors in D, but it seems they don't have a delete function (only for associative arrays), which is more of a limitation on a vector, so I'm wondering if I have it right. If a has the following array,

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

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

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

But this does not seem right (but maybe only because I do not understand this yet). So, is there a vector and is there another way to remove an element from a dynamic array?

Thanks.

+7
arrays vector d
source share
2 answers

You can use std.algorithm.remove() , which works not only with arrays, but also with universal ranges. Example:

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

There is an Array!T template in std.container that seems to be similar to std::vector from C ++.

 Array!int a = [0, 1, 2, 3]; a.linearRemove(a[1..3]); assert(equal(a, [0, 3])); 

Unfortunately, it does not have an individual removal method, although you can always use linearRemove with a Singleton range.

+3
source share

All Articles