开发者

Binary Search in D 2.0 (Phobos)?

开发者 https://www.devze.com 2023-02-03 04:06 出处:网络
Is it just me, or is there no binary search function in Phobos? I have a pre-sorted array that I want 开发者_运维知识库to search with my own comparator function, but I can\'t find anything in std.algo

Is it just me, or is there no binary search function in Phobos? I have a pre-sorted array that I want 开发者_运维知识库to search with my own comparator function, but I can't find anything in std.algorithms or std.containers.

Thanks!


Use SortedRange from std.range:

Cribbed from http://www.digitalmars.com/d/2.0/phobos/std_range.html#SortedRange:

auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a);
assert(r.canFind(3));
assert(!r.canFind(32));
0

精彩评论

暂无评论...
验证码 换一张
取 消