any_of(vs2010版本)
- 引言
这是我学习总结的<algorithm>库中的第三个函数any_of.这个函数和第一篇all_of可以相互学习,我感觉这两个可以形成互补的形式,犹如正命题和反命题。
- 作用
any_of的作用是检测容器中是否存在符合自定义条件的元素,如果存在,则返回true;否则返回false。而all_of 的作用是检测所有元素都是否符合自定义条件,至于两个的区别,我就不多说了。
- 实验
- 代码
test.cpp
#include <iostream> // std::cout
#include <algorithm> // std::any_of
#include <array> // std::array
// function declare
bool Condition( int i );
int main () {
std::array<int,7> foo = {0,1,-1,3,-3,5,-5};
if ( std::any_of(foo.begin(), foo.end(), Condition) )
std::cout << "There are some elements that equal 0 in the range.\n";
system("pause");
return 0;
}
bool Condition( int i )
{
return i == 0;
}