检查(未排序的)数组中是否存在值

时间:2019-01-31 21:11:57

标签: iterator range d

使用D语言in运算符,可以检查排序的随机访问范围中是否存在值。

但是,如果我想检查一个未排序且不是随机访问范围内的值是否存在,怎么办?

2 个答案:

答案 0 :(得分:2)

虽然我同意狼疮的说法,countUntil可以胜任,但是有一个功能可能开销更​​少,并且名称更合理:canFind

import std.algorithm.searching : canFind;
if (haystack.canFind(needle)) {
    // ...
}

答案 1 :(得分:1)

使用std.algorithm.searching : countUntil

import std.algorithm.searching : countUntil
if (array.countUntil(lookingFor) != -1) {
    // . . .
}

count Until就像其他许多语言中的indexOf一样。

https://dlang.org/phobos/std_algorithm_searching.html#countUntil