std::all_of() in C++ Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: CPP // 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"; } } Output: All numbers are even Time Complexity: O(n) where n is the size of the vector. Space Complexity: O(n) CPP // 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; } 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 More infoAdvertise with us Next Article std::all_of() in C++ M Manjeet Singh Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn 1 min read any_of() Function in C++ STL any_of() is the C++ function defined in <algorithm> library in STL. This function determines whether even one element in a given range satisfies a specified criterion. If at least one element meets the property, then it returns true; otherwise, it returns false. Also if the range is empty then 2 min read std::any Class in C++ any is one of the newest features of C++17 that provides a type-safe container to store single value of any type. In layman's terms, it is a container which allows one to store any value in it without worrying about the type safety. It acts as an extension to C++ by mimicking behaviour similar to an 6 min read Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam 3 min read Like