The C++ function is defined in **<algorithm>** library in STL. This function operates on whole range of array elements and can save time to run a loop to check each elements one by one. It checks for a given property on every element and returns true when each element in range satisfies specified property, else returns false.

**Syntax: **

template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);first :Input iterators to the initial positions in a sequence.last :Input iterators to the final positions in a sequence.pred :An unary predicate function that accepts an element and returns a bool.

**Exception :** Throws exception if either predicate or an operation on an iterator throws exception.

**Examples:**

`// C++ code to demonstrate working of all_of() ` `#include <vector> ` `#include <algorithm> ` `#include <iostream> ` `int` `main() ` `{ ` ` ` `std::vector<` `int` `> v(10, 2); ` ` ` ` ` `// illustrate all_of ` ` ` `if` `(std::all_of(v.cbegin(), v.cend(), [](` `int` `i){ ` `return` `i % 2 == 0; })) ` ` ` `{ ` ` ` `std::cout << ` `"All numbers are even\n"` `; ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

Output:

All numbers are even

`// C++ code to demonstrate working of all_of() ` `#include<iostream> ` `#include<algorithm> // for all_of() ` `using` `namespace` `std; ` `int` `main() ` `{ ` ` ` `// Initializing array ` ` ` `int` `ar[6] = {1, 2, 3, 4, 5, -6}; ` ` ` ` ` `// Checking if all elements are positive ` ` ` `all_of(ar, ar+6, [](` `int` `x) { ` `return` `x>0; })? ` ` ` `cout << ` `"All are positive elements"` `: ` ` ` `cout << ` `"All are not positive elements"` `; ` ` ` ` ` `return` `0; ` ` ` `} ` |

*chevron_right*

*filter_none*

Output:

All are not positive elements

In the above code, -6 being a negative element negates the condition and returns false.

Useful array algorithms STL functions

This article is contributed by **Manjeet Singh **. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- C++ Program for the Fractional Knapsack Problem
- Lexicographical smallest number after at most K consecutive swaps
- Important functions of STL Components in C++
- Check if Array elements can be maximized upto M by adding all elements from another array
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- std::remove_const in C++ with Examples
- All possible values of floor(N/K) for all values of K
- Finding Median of unsorted Array in linear time using C++ STL
- std::string::crbegin() and std::string::crend() in C++ with Examples
- std::is_trivially_copy_assignable class in C++ with Examples
- Find the K closest points to origin using Priority Queue
- How to iterate through a Vector without using Iterators in C++