VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdall_of-in-cpp/

⇱ std::all_of() in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

std::all_of() in C++

Last Updated : 23 Jul, 2025

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: 

Output:

All numbers are even

Time Complexity: O(n) where n is the size of the vector.

Space Complexity: O(n)

Output:

All are not positive elements

Time Complexity: O(n) where n is the size of the vector.

Space Complexity: O(n)

In the above code, -6 being a negative element negates the condition and returns false. Useful array algorithms STL functions

Comment
Article Tags: