Algorithm All Of Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm All Of Function

Algorithm All Of Function

BLUF: Mastering Algorithm All Of Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm All Of Function

C++ is renowned for its efficiency. Learn how Algorithm All Of Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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);

Example


## 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;

}

Example


Output:

All the array elements are odd.

Example


## 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;

}

Example


Output:

All elements are not positive

Example


## 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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience