In Java, If I want to know if an array contains a specific value, I only need to say
if (array.contains(value)) {};
Is there an equivalent to this in C++? A macro perhaps?
Thanks,
Mike
In Java, If I want to know if an array contains a specific value, I only need to say
if (array.contains(value)) {};
Is there an equivalent to this in C++? A macro perhaps?
Thanks,
Mike
You are not specific about the array data type.
If the array is a c-sting (array of characters terminated by a nul character) then there are string functions to do this (although they would do the looping for you).
If the array is just an array of values (eg, integers) then in C/C++ looping is your only option.
I'm no expert in Java, but I think in Java an array is a collection (class) which, again, would be looping in the implementation of the method to find the match for you.
Is there an equivalent to this in C++? A macro perhaps?
You could, of course, always write your own function to do it
marco_c:
You are not specific about the array data type.If the array is a c-sting (array of characters terminated by a nul character) then there are string functions to do this (although they would do the looping for you).
If the array is just an array of values (eg, integers) then in C/C++ looping is your only option.
I'm no expert in Java, but I think in Java an array is a collection (class) which, again, would be looping in the implementation of the method to find the match for you.
Apologies, in this case, the Array contains datatype unsigned long
It might be worth pointing out that when you "if (array.contains(value))" in java, you merely invoke some existing code that will end up ... looping through the array. There are, in theory, C++ libraries that could do the same thing (or be written to do the same thing), but Arduino doesn't include them by default, presumably to cut down on complexity.
BTW, are you sure that array.contains() exists in Java? A quick search shows that "contains" works on "lists", which are not quite the same as arrays. To search an array, examples show:
Arrays.asList(myArray).contains(value);
Which is probably a fine example of the sort of complexity that Arduino is trying to avoid!