C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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]. Syntaxtemplate <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameterfirst: 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 valueThe 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#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; } Output: All the array elements are odd. 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; } Output: All elements are not positive ComplexityThe 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 racesEither the function accesses all the objects in the specified range or some of them. ExceptionsThe function throws an exception if any of the argument throws one.
Next TopicC++ Algorithm copy_backward Function
|