Initialization of dynamic multidimensional array

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 6 12:24:34 PST 2017


One another way is use something like this:
import std.array, std.algorithm, std.stdio;
auto arr = uninitializedArray!(int[][])(ROWS,COLS);
arr.each!"a[]=-1";
writeln(arr);

Dne 6. 2. 2017 8:21 PM napsal uĹživatel "berni via Digitalmars-d-learn" <
digitalmars-d-learn at puremagic.com>:

> On Sunday, 5 February 2017 at 21:14:33 UTC, Daniel Kozak wrote:
>
>> http://stackoverflow.com/questions/24600796/d-set-default-
>> value-for-a-struct-member-which-is-a-multidimensional-
>> static-arr/24754361#24754361
>>
>>
>> Dne 5.2.2017 v 21:33 berni via Digitalmars-d-learn napsal(a):
>>
>>> With X not known at compile time:
>>>
>>> auto arr = new int[][](X,X);
>>>> for (int i=0;i<X;i++)
>>>>   for (int j=0;j<X;j++)
>>>>     arr[i][j] = -1;
>>>>
>>>
>>> Is there anything better for this? I mean, the program will fill the
>>> array with zeroes, just to overwrite all of them with -1. That's wasted
>>> execution time and doesn't feel D-ish to me.
>>>
>>
> Thanks for the link. Looks a little bit hacky... I hoped for something
> more straightforward, or at least, if direct initialisation is not possible
> something like "arr[][] = -1;" or "arr[0..X][0..X] = -1;" But as far as I
> can see now, this does not exist.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170206/76d0d614/attachment.html>


More information about the Digitalmars-d-learn mailing list