Fastest Way to Append Multiple Elements to an Array

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 14 16:18:27 PST 2014


On Sunday, 14 December 2014 at 23:52:15 UTC, zeljkog wrote:
> On 15.12.14 00:16, "Nordlöw" wrote:
>> or some other trick?
>
> void append(T, Args...)(ref T[] arr, Args args){
>   arr.length += args.length;
>   foreach(i, e; args)
>     arr[$ - args.length + i] = args[i];
> }
>
>
> void main()
> {
>   int[] arr = [1, 2, 3, 4, 5];
>   arr.append(3, 10, 14);
>   writeln(arr);
> }
>
> output:
> [1, 2, 3, 4, 5, 3, 10, 14]

Maybe *std.array.Appender* should be used in append().

(http://dlang.org/phobos/std_array.html#.Appender)

Even if i often myself feel too lazy to use it and use only the 
concat. op instead.



More information about the Digitalmars-d-learn mailing list