如果'pred'参数的值为true,则C++算法all_of()函数将返回true值。对于[first,last]参数内的所有元素,该值均应为true。
template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
first - 它指定列表中的第一个元素。
last - 它指定列表中的最后一个元素。
pred - 这是一元函数,可以接受参数内的参数。
该函数具有一种返回类型" true"。如果参数'pred'的值对于参数中的所有元素都是true,则返回值'true',否则返回false。
#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; }
输出:
All the array elements are odd.
#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; }
输出:
All elements are not positive
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)