C++算法any_of()函数对参数中的每个元素测试'pred'的值,如果对于任何元素pred的值为true,则该函数返回true,否则返回false。
template <class InputIteratir, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
first - 它是指定参数内的第一个元素。
last - 它是参数中的最后一个元素。
pred - 这是一元函数,可以接受参数内的参数。
该函数具有一种返回类型" true"。如果参数'pred'的值对于该参数的任何元素为true,则返回值'true',否则返回false。
#include <iostream> #include <algorithm> #include <array> using namespace std; int main() { int arr[7] = {2,4,6,5,10,3,14}; any_of(arr,arr+6, [](int k){return k%2;})? cout <<"There are elements which exist in the table of 2": cout<<"No elements in the table of 2 exists"; return 0; }
输出:
There are elements which exist in the table of 2.
#include <iostream> #include <algorithm> #include <array> int main() { std::array<int, 5> arr = {2,-4,6,-9,10}; if(std::any_of (arr.begin(), arr.end(), [](int k) { return k<0;})) std::cout <<"Negative elements exist in the array"; return 0; }
输出:
Negative elements exist in the array
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
HarmonyOS快速入门与实战 -〔QCon+案例研习社〕