template <class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);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.
If [first,last)
is an empty range, the function returns false
.
如果pred判断条件对任意元素成立,则返回true,否则返回false,如果[first,last)是一个空集,则返回false。
The behavior of this function template is equivalent to:
这一行为和下面的函数模板类似:
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;
}
Example
例子
// any_of example
#include <iostream> // std::cout
#include <algorithm> // std::any_of
#include <array> // std::array
int main () {
std::array<int,7> foo = {0,1,-1,3,-3,5,-5};
if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are negative elements in the range.\n";
return 0;
}