Home » C++ algorithm all_of() function

C++ algorithm all_of() function

by Online Tutorials Library

C++ Algorithm Functions all_of()

C++ Algorithm all_of() function returns a true value if the value of ‘pred’ argument is true. The value should be true for all elements in the range [first, last].

Syntax

Parameter

first: It specifies the first element in the list.

last: It specifies the last element in the list.

pred: It is a unary function which accepts the argument from the range.

Return value

The function has one return type, ‘true’. If the value of argument ‘pred’ is true for all the elements of the range then the value ‘true’ is returned, else false.

Example 1

Output:

All the array elements are odd.  

Example 2

Output:

All elements are not positive   

Complexity

The function moves linearly, starting from the first element going towards the last one. For each element of the list, the value of ‘pred’ is checked. The search goes on until a mismatch for the ‘pred’ value is encountered.

Data races

Either the function accesses all the objects in the specified range or some of them.

Exceptions

The function throws an exception if any of the argument throws one.

You may also like