The return of std.algorithm.find

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 15 01:56:22 PST 2016


15.11.2016 12:50, RazvanN пишет:
> On Tuesday, 15 November 2016 at 09:43:27 UTC, RazvanN wrote:
>> The find function which receives an input haystack and a needle
>> returns the haystack advanced to the first occurrence of the needle.
>> For normal ranges this is fine, but for
>> sorted ranges (aka SortedRange) it is a bit odd. For example:
>>
>> find(assumeSorted[1, 2, 4, 5, 6, 7], 4) would return [4, 5, 6, 7].
>> This is in terms with the general policy of the find function, but is
>> weird. Since we know the range is sorted, shouldn't the result be [1,
>> 2, 4]?
>
> The whole function is find!"a <= b"(assumeSorted([1, 2, 4, 5, 6, 7]),
> 4). And the result is
> the whole range [1, 2, 4, 5, 6, 7].
IIRC find!"a <= b" will return the first element that is less or equal 
to needle, so this will be the whole range. To find all elements of 
SortedRange that are less or equal to needle you need to use 
SortedRange.lowerBound
http://dlang.org/phobos/std_range.html#.SortedRange.lowerBound


More information about the Digitalmars-d-learn mailing list