Check if Any element in Array matches a condition in C++

This tutorial will discuss about a unique way to check if any element in array matches condition in C++.

To check if any element in array matches a condition, we need to iterate over all the elements of array, and during iteration we need to check the condition for each element. Although we can do this using a for-loop, but it is best to use an existing STL Algorithm i.e. std::any_of().

The std::any_of() function accepts three arguments,

  • Iterator pointing to the start of a sequence.
  • Iterator pointing to the end of a sequence.
  • A Callback or Lambda function which accepts a value of same type as the type of sequence, and returns a boolean value.

The std::any_of() function will apply the given Lambda function on all the elements of the sequence and it returns true if the callback function returns true for any element of sequence.

Now this instead of a Lambda function, we can also pass a global function or a member function of a class. This function will represents our condition. Using ats::any_of() function we can check if the given Lambda function returns true for any element of array.

For example, we want to check if any integer in array is an odd number? For this, we will use this code,

int arr[] = {46, 76, 18, 92, 23, 67, 98};

// Get the size of array
size_t len = sizeof(arr) / sizeof(arr[0]);

// A Condition function
// It checks if a given element is Odd
auto conditionFunc = [](int elem) {
                        return elem % 2 == 1;
                        };

bool result = std::any_of(arr, arr + len, conditionFunc);

We passed three arguments in the std::any_of() function,

  • Iterator pointing to the start of array i.e. iterator returned by std::begin(arr).
  • Iterator pointing to the end of array i.e. iterator returned by std::end(arr).
  • A Lambda function which accepts an integer as an argument, and returns true if the given number is odd number.

The any_of() function will return true, if there is any odd number in the array. This way, by encapsulating our condition in a lambda function, we can check if any element in array matches a given condition.

Let’s see the complete example,

#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = {46, 76, 18, 92, 23, 67, 98};

    // Get the size of array
    size_t len = sizeof(arr) / sizeof(arr[0]);

    // A Condition function
    // It checks if a given element is Odd
    auto conditionFunc = [](int elem) {
                            return elem % 2 == 1;
                         };

    if ( std::any_of(arr, arr + len, conditionFunc) )
    {
        std::cout << "Yes, at least one element of Array arr satisfies the condition.\n";
    }
    else
    {
        std::cout << "No, none of the Array element satisfies the condition.\n";
    }

    return 0;
}

Output :

Yes, at least one element of Array arr satisfies the condition.

Summary

Today we learned about several ways to check if any element in array matches condition in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top