Predicates 判断式
来源:互联网 发布:站长工具 域名备案 编辑:程序博客网 时间:2024/06/05 06:40
算法中有一种特殊的辅助函数叫做predicates(判断式)。所谓predicates,就是返回值为bool的函数。它们通常被用来指定排序准则和搜寻准则。Predicates可能有一个或者是两个操作数,视具体情况而定。注意,并非任何返回布尔值的一元函数或二元函数就是合法的predicate。STL要求,面对相同的值,predicates必须得出相同的结果。这些戒律将那些“被调用时,会改变自己的内部状态”的函数清楚出场。
Unary Predicates(一元判断式)
Unary Predicates会检查唯一参数的某项特性。典型例子是像下面这样的函数,用来搜寻第一个质数。
<span style="font-size:18px;">#include <iostream>#include <list>#include <algorithm>#include <cstdlib>using namespace std;bool isPrime(int number){number = abs(number);if (number == 0 || number == 1){return true;}int divisor;for (divisor = number / 2; number % divisor != 0; --divisor){;}return divisor == 1;}int main(){list<int> coll;for (int i = 24;i <= 30; ++i){coll.push_back(i);}list<int>::iterator pos;pos = find_if(coll.begin(), coll.end(), isPrime);if (pos != coll.end()){cout << *pos << "is first prime number found" << endl;}else{cout << "no prime number found " << endl;}return 0;}//输出//29 is first prime number found</span>
Binary Predicates 二元判断式
Binary predicates的典型用途是,比较两个参数的特定属性。例如,为了依照你自己的原则对元素进行排序,你必须以一个简单的predicate形式提供这项原则。如果元素本身不支持operator<,或如果你想使用不同的排序原则,这就派上用场了。<span style="font-size:18px;">bool checkEven(int elem, bool even){if (even){return elem % 2 == 0;}else{return elem % 2 == 1;}}int main(){vector<int> coll;INSERT_ELEMENTS(coll, 1, 9);PRINT_ELEMENTS(coll, "coll:");bool checkEvenArgs[3] = {true, false, true};vector<int>::iterator pos;pos = search(coll.begin(), coll.end(), checkEvenArgs, checkEvenArgs + 3, checkEven);while (pos != coll.end()){cout << "subrange found starting with element"<< distance(coll.begin(), pos) + 1<< endl;pos = search(++pos, coll.end(), checkEvenArgs, checkEvenArgs + 3, checkEven);}return 0;}</span>
0 0
- Predicates 判断式
- Predicates
- Predicates
- LINQ to Entities: Combining Predicates(组合判断)
- 隐式转换在执行计划中对Access predicates 和Filter predicates 的影响
- Java中函数式编程的谓词函数(Predicates)
- Comparison Predicates
- predicates 案例
- 两个几何谓词判断(predicates)的原理:点与直线的关系&点与圆的关系
- Dynamically Composing Expression Predicates
- predicates (from functional.h)
- Predicates of Foundation Framework
- Cocoa Predicates Classes
- mountebank之Predicates
- 谓词函数predicates和仿函数functors
- 关于谓词函数predicates的介绍
- Effective STL 39 Make predicates pure functions
- Linq复杂查询 LINQ to Entities: Combining Predicates
- ReactiveCocoa的常见用法和常用宏
- 蓝桥杯——十六进制转十进制
- 后台线程
- CodeForces 7C 【数论】
- hive sql基本操作
- Predicates 判断式
- python 根据类的字符串名实例化对象
- MessageUI.framework发短信
- 多线程两种方式对比与区别
- This is it——Halloween
- android Acitivity之间的数据传递
- tableview 下拉图片放大
- 32位和64位系统的原理
- nodejs moment and cassandra 日期存储