Associative array of dynamic array

monarch_dodra monarchdodra at gmail.com
Sun Sep 9 05:29:41 PDT 2012


On Sunday, 9 September 2012 at 12:17:47 UTC, Samuele Carcagno 
wrote:
> I would like to create an associative array that maps a string 
> to a multidimensional dynamic array. In other words, I would 
> like a data structure where I can access elements like this:
>
> foo["key"][0][0]
>
> Is this possible? If so I'm having trouble figuring out the 
> correct syntax, the following (for one dimension) doesn't work:
>
> auto foo = new int[5][string];
>
> compilation fails with the following message:
> Error: cannot implicitly convert expression (string) of type 
> string to ulong
>
> Thanks for any help!

Other way round. Unlike C++, everything goes before the 
identifier:

--------
import std.stdio;
void main()
{
     int[][][string] foo;
     foo["hello"]=[[1],[2]];
     writeln(foo["hello"]);
     writeln(foo["hello"][0][0]);
}
-------
[[1], [2]]
1
-------
foo is an associative array, the key is "string", and the type is 
"int[][]"


More information about the Digitalmars-d-learn mailing list