TCG TCG - 5 months ago 18
C++ Question

Check if elements in an array are inside of another array

So If I have these two arrays:

int array1[] = {1, 2 ,3};

int array2[] = {1, 2, 3, 4, 5};

How do I check if
1, 2 and 3
from array1 are in array2?

In python I could do:

if array1 in array2:
# code

Thanks in advance.



if (std::includes(std::begin(array2), std::end(array2),
                  std::begin(array1), std::end(array1)) {
    // array2 includes array1

This requires the arrays are sorted, which yours are. Also, if they are sorted with some custom comparator, you must pass that to std::includes as well.

It is worth pointing out that I use your arrays the "wrong" way round; the algorithm expects its first range to be the larger one.