(1)any_of
函数说明:
any_of(开始,结束,条件函数(fn)):
用法:在[ 开始,结束)这个区间, 判断是否有一个元素满足 fn;
<any_of 示例>
#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;
}
函数定义:
参数及其返回值: first,last
分别指向一个序列中初始及末尾位置的输入迭代器。这个范围即 [first,last) ,包括 first 到 last 间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
pred:
一元谓词(Unary)函数,以范围中的一个元素为参数,然后返回一个可转换成 bool 类型的值,其返回值表明指定元素是否满足当前函数所检测的条件。 该函数不能修改其参数。
可以是函数指针(Function pointer)类型或函数对象(Function object)或 lambda bind 等类型。
返回值:
当范围中存在某个元素传入 pred 后返回 ture,则 any_of 返回 ture,否则返回 false。
当范围为空,返回 false。
(2)函数说明:none_of
none_of(开始,结束,条件函数fn):
用法:在 [开始,结束)这个区间,所有元素都不满足fn,返回true ,否则返回false。
<none_of 示例>
#include <iostream> // std::cout
#include <algorithm> // std::none_of
#include <array> // std::array
int main () {
std::array<int,8> foo = {1,2,4,8,16,32,64,128};
if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are no negative elements in the range.\n";
return 0;
}
(3)函数说明:all_of
all_of(开始,结束,函数fn):
用法:在[开始,结束)这个所有元素都满足fn 则返回ture,否则返回false。
<all_of 示例>
#include <iostream> // std::cout
#include <algorithm> // std::all_of
#include <array> // std::array
int main () {
std::array<int,8> foo = {3,5,7,11,13,17,19,23};
if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
std::cout << "All the elements are odd numbers.\n";
return 0;
}
(4)Lambda表达式
Lambda表达式允许你在本地定义函数,即在调用的地方定义,从而消除函数对象产生的许多安全风险,Lambda表达式的格式如下:
[capture](parameters)->return-type {body}
[]里是函数调用的参数列表,表示一个Lambda表达式的开始