Best way to check for an element in an array?
FrankLike via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Apr 23 05:39:01 PDT 2014
Hi,everyone,
This code must add the 'break',
import std.stdio;
int x=0;
template isin(T){
bool isin(T[] Array,T Element){
bool rtn=false;
foreach(T ArrayElement; Array){
if(Element==ArrayElement){
rtn=true; break; ← //here add break
}
x++;
}
return rtn;
}
}
void main(string[] args)
{
int[] stuff=[0,1,2,3,4,5,6,7,8,9,10]; ← //here declare int[]
if (stuff.isin(2)) // Much clean!
{
writeln(x);
writeln("Hello World!");
}
}
----------------end---------------
Frank
More information about the Digitalmars-d-learn
mailing list