Simplest way to create an array from an associative array which its contains keys and values?

Jakob Ovrum jakobovrum at gmail.com
Tue Jan 7 14:39:10 PST 2014


On Tuesday, 7 January 2014 at 20:38:11 UTC, Craig Dillabaugh 
wrote:
> Saves a few lines of code, and looks cooler, it seems that the 
> trivial foreach loop version is very easy:
>
> string[] array;
>
> foreach (key, value; aa) {
> 	array ~= key;
> 	array ~= value;
> }

Assuming the return value of `byKey` has a length property, using 
std.array.array has the benefit of automatically allocating just 
once for the result, while your proposed alternative will grow on 
demand, causing several allocations that depend on the number of 
pairs in `aa`.

Of course, Teoh's `aaToArray` can trivially do this too, but the 
naive loop straight in user code is just that - naive.


More information about the Digitalmars-d-learn mailing list