作用: Test if any element in range fulfills condition
Returns true
if pred returns true
for any of the elements in the range [first,last)
, and false
otherwise.
对所有元素进行条件判断, 若任意一个成立则返回true, 都不成立则返回false, 元素为空时会返回true。
时间复杂度:
应用:
int nums[5] = {1,2,3,4,5};
if(any_of(nums, nums + 5, [](int i){return i%2;}))
cout << "存在至少一个奇数\n";
函数原型
template<class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return true;
++first;
}
return false;
}