any_of() in C++

any_of(): This function checks that a predicate is false for at least an element of 

sequence. This function returns true if the check is true, false otherwise. 

Returns true if an element exists in the range * [__first,__last) such that __pred is 

true, and false otherwise.

Parameters: Three parameters are required for this function.

 __first – An input iterator.

 __last – An input iterator.

 __pred – A predicate. 

Syntax:

any_of(__first, __last, __pred)

For Example:

arr = {2,4,12,11}

any_of(arr.begin(), arr.end(), [](int i) {return i&1;)) = > It returns true because one 

element in the array is odd.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intarr = {241211};

    if (any_of(arr.begin(), arr.end(), [](int i)
               { return i & 1; }))
        cout << "Any element in the array is odd\n";

    else
        cout << "No element in the array is odd\n";

    return 0;
}


No comments:

Post a Comment