www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can convert the folowing to D.

reply learner <learner learner.com> writes:
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.
Mar 31 2016
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/31/2016 05:34 PM, 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.
import std.stdio; void main() { int[] a = [ 1, 2, 3, 4, 5 ]; writefln("Before: %s", a); size_t index = 2; a = a[index .. $]; // <-- HERE writefln("After : %s", a); } Note that it's a cheap operation; the elements are still in memory and not destroyed. If you want to run their destructors you can call destroy() explicitly: import std.stdio; import std.algorithm; import std.range; struct Rect { int i; ~this() { writefln("Destroying %s", i); } } void main() { Rect[] a = iota(5).map!(i => Rect(i)).array; writefln("Before: %s", a); size_t index = 2; // If you need to run the destructors now: a[0 .. index].each!((ref e) => e.destroy); a = a[index .. $]; writefln("After : %s", a); } Prints Destroying 0 Destroying 1 Destroying 2 Destroying 3 Destroying 4 Before: [Rect(0), Rect(1), Rect(2), Rect(3), Rect(4)] Destroying 0 Destroying 1 After : [Rect(2), Rect(3), Rect(4)] Ali
Mar 31 2016
parent learner <learner learner.com> writes:
On Friday, 1 April 2016 at 01:09:32 UTC, Ali Çehreli wrote:
 On 03/31/2016 05:34 PM, 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.
import std.stdio; void main() { int[] a = [ 1, 2, 3, 4, 5 ]; writefln("Before: %s", a); size_t index = 2; a = a[index .. $]; // <-- HERE writefln("After : %s", a); } Note that it's a cheap operation; the elements are still in memory and not destroyed. If you want to run their destructors you can call destroy() explicitly: import std.stdio; import std.algorithm; import std.range; struct Rect { int i; ~this() { writefln("Destroying %s", i); } } void main() { Rect[] a = iota(5).map!(i => Rect(i)).array; writefln("Before: %s", a); size_t index = 2; // If you need to run the destructors now: a[0 .. index].each!((ref e) => e.destroy); a = a[index .. $]; writefln("After : %s", a); } Prints Destroying 0 Destroying 1 Destroying 2 Destroying 3 Destroying 4 Before: [Rect(0), Rect(1), Rect(2), Rect(3), Rect(4)] Destroying 0 Destroying 1 After : [Rect(2), Rect(3), Rect(4)] Ali
thanks
Apr 01 2016
prev sibling parent ZombineDev <petar.p.kirov gmail.com> writes:
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.
Apr 01 2016