digitalmars.D.learn - Help Converting from C++
- rookie (24/24) Jul 29 2012 I have the following code.
- bearophile (32/35) Jul 29 2012 Do you want something like this?
- bearophile (7/10) Jul 29 2012 In the D Phobos there is no efficient stack yet, so I've used a
- rookie (5/15) Jul 29 2012 Hi Bearophile,
I have the following code. Heavily cutdown. How would I do the same thing in D? ////////////////////////////// class NInfo { public: Stack<int> info; } int cols = 5; int rows = 10; ninfo = new NInfo*[cols+2]; int r; for (r=0; r<cols+1; r++) { ninfo[r]=new NInfo[rows+2]; for (int c=0; c<rows+1; c++) { ninfo[r][c].info.push(r); ninfo[r][c].info.push(c); } } Rookie
Jul 29 2012
rookie:I have the following code. Heavily cutdown. How would I do the same thing in D?Do you want something like this? struct NInfo { public int[] info; } void main() { int cols = 3; int rows = 5; auto nInfo = new NInfo[][](rows + 2, cols + 2); foreach (r, row; nInfo) foreach (c, ref item; row) item.info ~= [r, c]; // not so efficient import std.stdio; writefln("[%([%(%s, %)],\n %)]]", nInfo); } Output: [[NInfo([0, 0]), NInfo([0, 1]), NInfo([0, 2]), NInfo([0, 3]), NInfo([0, 4])], [NInfo([1, 0]), NInfo([1, 1]), NInfo([1, 2]), NInfo([1, 3]), NInfo([1, 4])], [NInfo([2, 0]), NInfo([2, 1]), NInfo([2, 2]), NInfo([2, 3]), NInfo([2, 4])], [NInfo([3, 0]), NInfo([3, 1]), NInfo([3, 2]), NInfo([3, 3]), NInfo([3, 4])], [NInfo([4, 0]), NInfo([4, 1]), NInfo([4, 2]), NInfo([4, 3]), NInfo([4, 4])], [NInfo([5, 0]), NInfo([5, 1]), NInfo([5, 2]), NInfo([5, 3]), NInfo([5, 4])], [NInfo([6, 0]), NInfo([6, 1]), NInfo([6, 2]), NInfo([6, 3]), NInfo([6, 4])]] Bye, bearophile
Jul 29 2012
struct NInfo { public int[] info; }In the D Phobos there is no efficient stack yet, so I've used a dynamic array. If you need to append many items use an appender or use some truly segmented stack data structure. Also if NInfo must really be a class, things become a little more complex. Bye, bearophile
Jul 29 2012
Hi Bearophile, What you gave is perfect. Thanks, Rookie On Sunday, 29 July 2012 at 18:06:29 UTC, bearophile wrote:struct NInfo { public int[] info; }In the D Phobos there is no efficient stack yet, so I've used a dynamic array. If you need to append many items use an appender or use some truly segmented stack data structure. Also if NInfo must really be a class, things become a little more complex. Bye, bearophile
Jul 29 2012