The C++ algorithm function all_of will output true if the 'pred' argument evaluates to true for every element within the specified range [first, last].
Syntax
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Parameter
It designates the initial element within the list.
last : It specifies the last element in the list.
The ```
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
## Return value
The function returns a single value, 'true'. When the argument 'pred' evaluates to true for every element within the range, the function will return 'true'; otherwise, it will return false.
## Example 1
include<iostream>
include<algorithm>
include<array>
int main
{
std::array<int, 6> arr= {25,27,29,31,33,35};
if ( std::all_of(arr.begin, arr.end, (int k) {return k%2;} ) )
std::cout <<"All the array elements are odd.";
return 0;
}
Output:
All the array elements are odd.
## Example 2
include<iostream>
include<algorithm>
using namespace std;
int main
{
int ar[6] = {2, 5, -7, -9, 3, 5};
all_of(ar, ar+6, (int x) { return x>0; })?
cout<<"All elements are positive \n":
cout<<"All elements are not positive";
return 0;
}
Output:
All elements are not positive
## Complexity
The process progresses sequentially, commencing at the initial element and proceeding towards the final one. Each item in the list undergoes evaluation for the 'pred' value. The iteration continues until a discrepancy in the 'pred' value is detected.
## Data races
Either the function interacts with every object within the designated range or a subset of those objects.
## Exceptions
The function will raise an exception if any of the arguments also raises an exception.