How can convert the folowing to D.
ZombineDev via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 1 11:12:58 PDT 2016
On Friday, 1 April 2016 at 00:34:49 UTC, learner wrote:
> Hi,
>
> I have the following code in C++.
>
> rectangles.erase(rectangles.begin() + index);
>
> where rectangles is:
> std::vector<Rectangle> rectangles;
>
> how can I do something similar in D.
>
> Learner.
Also, if you are using std.container.array (which similar to
std::vector in C++), you can use its linearRemove method, by
giving it a range to remove (a slice of itself), like this:
import std.container.array;
import std.range : iota, drop, take;
import std.stdio : writeln;
void main()
{
auto arr = Array!int(10.iota); // initialize with [0, 10)
arr[].writeln();
// same as arr.linearRemove(arr[5 .. 7]);
arr.linearRemove(arr[].drop(5).take(2));
arr[].writeln();
}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 7, 8, 9]
The arr[] syntax is just a shorthand of arr.opSlice() which
returns a range iterating over the elements of the array. Given
this range, you can use the algorithms from
http://dlang.org/phobos/std_range to perform further
manipulations.
More information about the Digitalmars-d-learn
mailing list