Efficiently streaming data to associative array

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 9 06:36:46 PDT 2017


On 8/8/17 3:43 PM, Anonymouse wrote:
> On Tuesday, 8 August 2017 at 16:00:17 UTC, Steven Schveighoffer wrote:
>> I wouldn't use formattedRead, as I think this is going to allocate 
>> temporaries for a and b.
> 
> What would you suggest to use in its stead? My use-case is similar to 
> the OP's in that I have a string of tokens that I want split into 
> variables.

using splitter(","), and then parsing each field using appropriate 
function (e.g. to!)

For example, the OP's code, I would do:

auto r = line.splitter(",");
a = r.front;
r.popFront;
b = r.front;
r.popFront;
c = r.front.to!int;

It would be nice if formattedRead didn't use appender, and instead 
sliced, but I'm not sure it can be fixed.

Note, one could make a template that does this automatically in one line.

-Steve


More information about the Digitalmars-d-learn mailing list