'in' syntax for searching arrays

Oskar Linde oskar.lindeREM at OVEgmail.com
Sun Nov 4 10:55:30 PST 2007


Matti Niemenmaa wrote:
> Janice Caron wrote:
>> V[] a;
>> int n = a.find(x);
>>
>> should return -1 if the value is not present in the array, or the
>> index if it is. This is a much better idea than returning a pointer to
>> the element, because (a) you don't have to worry about const and
>> invariant, and (b) it's the same as std.string. In addition, you can
>> then do a[n..$].find() to find the second occurence, and so on.
> 
> As an aside: Tango uses the idiom that find returns the length of the given
> array if no match is found. This allows it to return a size_t (if you ever use
> arrays longer than int.max...) whilst still having a clear error value.

It is also helpful that arr.length is a valid slice delimiter, so that 
you with Tango:

a[a.find(whatever)..$] and
a[0..a.find(something)]

remain valid even when no match is found.

-- 
Oskar



More information about the Digitalmars-d mailing list