all_of() in C++

all_of(): This function checks that a predicate is true for all the elements of a 

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

true if __pred is true for each element in the range [__first,__last), and false 

otherwise.

Parameters:

 __first – An input iterator.

 __last – An input iterator. 

__pred – A predicate.

Syntax:

all_of(__first, __last, __pred)

For Example:

arr = { 1,3,15,17}

all_of(arr.begin(),arr.end(), [] (int i) {return i&1;}))= > It returns true because all elements 

are odd.

Approach

C++

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

int main()
{
    vector<intarr = {131517};
    if (all_of(arr.begin(), arr.end(), [](int i)
               { return i & 1; }))
        cout << "All odd\n";
    else
        cout << "All are not odd\n";

    return 0;
}


No comments:

Post a Comment