Help Converting from C++

bearophile bearophileHUGS at lycos.com
Sun Jul 29 11:02:55 PDT 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


More information about the Digitalmars-d-learn mailing list