Avoiding const?

Jonathan M Davis jmdavisProg at gmx.com
Tue Feb 21 20:36:26 PST 2012


On Wednesday, February 22, 2012 04:42:05 ixid wrote:
> Thank you, I'll read those articles. Is there a more elegant way
> than this to get the string[] out of a range after using the
> algorithms sort? Ranges are a bit of a mystery.
> 
> 	string[] temp;
> 	foreach(i;sort(set.keys))
> 		temp ~= to!string(i);

Sort returns a SortedRange, so if you passed it to a function which could take 
advantage of that (such as find), it would be more efficient to use the result of 
sort, but sort sorts in place. So, if you want a new array, what you should 
probably do is simply

auto temp = set.keys.dup;
sort(temp);

However, I think that .keys returns a newly allocated array such that you 
don't need to dup it at all if you don't want to, which means that you'd do 
something more like

auto temp = set.keys;
sort(temp);

> I'm a little worried that the very basic level of my posts is
> spamming the forum, is there a D community forum for absolute
> beginners?

This pretty much _is_ the forum for absolute beginners. The whole point of 
this forum is to answer questions for those learning D. It's digitalmars.D 
which is for general discussion on D. You're in the right place.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list