Vectors and dynamic arrays in D
I was thinking that dynamic arrays wer开发者_Go百科e 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.
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]);
}
In std.container
there is an Array!T
template, which appears to be much like 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 appear to have an individual remove method, although you could always use linearRemove
with a singleton range.
精彩评论