Binary Predicate(双参判断式)的用途是:比较两个参数的特定属性

我们先建一个领域模型类:

Person.h

#ifndef _Domain_Models_Person_H_
#define _Domain_Models_Person_H_ #include <iostream>
#include <string>
#include <deque> using namespace std; class Person
{
private:
string fn; // first name
string ln; // last name
int age;
public:
Person() { }
Person(const string& f, const string& n)
: fn(f), ln(n)
{
}
string firstname() const;
string lastname() const;
int getAge() const;
void setAge(int a);
static bool sortByName(const Person& p1, const Person& p2);
static bool sortByAge(const Person& p1, const Person& p2);
static void sortDequeByName(deque<Person> &persons);
static void sortDequeByAge(deque<Person> &persons);
}; #endif

Person.cpp

#include <algorithm>
#include "Person.h" inline string Person::firstname() const
{
return fn;
} inline string Person::lastname() const
{
return ln;
} inline int Person::getAge() const
{
return age;
} void Person::setAge(int a)
{
age = a;
} /* binary function predicate:
* - returns whether a person is less than another person
*/
bool Person::sortByName(const Person& p1, const Person& p2)
{
/* a person is less than another person
* - if the last name is less
* - if the last name is equal and the first name is less
*/
return p1.lastname()<p2.lastname() ||
(p1.lastname() == p2.lastname() &&
p1.firstname()<p2.firstname());
} // another binary predicate
bool Person::sortByAge(const Person& p1, const Person& p2)
{
return p1.getAge() < p2.getAge();
} void Person::sortDequeByName(deque<Person> &persons)
{
// sort elements
sort(persons.begin(), persons.end(), // range
Person::sortByName); // sort criterion
} void Person::sortDequeByAge(deque<Person> &persons)
{
// sort elements
sort(persons.begin(), persons.end(), // range
Person::sortByAge); // sort criterion
}

然后,我们需要测试下我们的领域模型

为了方便输出模型,先给我们的测试类加入运算符重载的友元函数

class PredicateTest : public TestBase
{
friend ostream& operator<< (ostream& s, const Person& p);
...  
}

实现如下:

ostream& operator<< (ostream& s, const Person& p)
{
s << "[" << p.lastname() << ", " << p.firstname() << ", " << p.getAge() << "]";
return s;
}

测试代码如下:

void PredicateTest::binaryPredicate()
{
// create some persons
Person p1("nicolai", "josuttis");
Person p2("ulli", "josuttis");
Person p3("anica", "josuttis");
Person p4("lucas", "josuttis");
Person p5("lucas", "otto");
Person p6("lucas", "arm");
Person p7("anica", "holle");
p1.setAge();
p2.setAge();
p3.setAge();
p4.setAge();
p5.setAge();
p6.setAge();
p7.setAge(); // insert person into collection coll
deque<Person> coll;
coll.push_back(p1);
coll.push_back(p2);
coll.push_back(p3);
coll.push_back(p4);
coll.push_back(p5);
coll.push_back(p6);
coll.push_back(p7); // print elements
cout << "deque before sort():" << endl;
deque<Person>::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << endl;
} cout << "-- Sort By Name --" << endl;
Person::sortDequeByName(coll); // print elements
cout << "deque after sort():" << endl;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << endl;
} cout << "-- Sort By Age --" << endl;
Person::sortDequeByAge(coll); // print elements
cout << "deque after sort():" << endl;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << endl;
}
}

运行结果:

---------------- binaryPredicate(): Run Start ----------------
deque before sort():
[josuttis, nicolai, 20]
[josuttis, ulli, 30]
[josuttis, anica, 18]
[josuttis, lucas, 2]
[otto, lucas, 22]
[arm, lucas, 35]
[holle, anica, 95]
-- Sort By Name --
deque after sort():
[arm, lucas, 35]
[holle, anica, 95]
[josuttis, anica, 18]
[josuttis, lucas, 2]
[josuttis, nicolai, 20]
[josuttis, ulli, 30]
[otto, lucas, 22]
-- Sort By Age --
deque after sort():
[josuttis, lucas, 2]
[josuttis, anica, 18]
[josuttis, nicolai, 20]
[otto, lucas, 22]
[josuttis, ulli, 30]
[arm, lucas, 35]
[holle, anica, 95]
---------------- binaryPredicate(): Run End ----------------

STL - Predicate - Binary Predicate(双参判断式)的更多相关文章

  1. STL - 判断式(Predicate) - 单参判断式(Unary Predicate)

    Predicate是一种特殊的辅助函数,它会返回Boolean,常常被用来作为排序或者查找准则. Predicate会有1个或者2个操作数. Unary Predicate(单参判断式) 例子: 我们 ...

  2. 【重点】Shell入门教程:流程控制(3)条件判断式的真假值

    之前曾提到,在Bash中什么是真什么是假,是以命令的结束状态是否为0来做判断.传回0,即为真:传回非0,即为假. 在Bash中,这种可以影响程序流程的式子,称为条件判断式.判断式的操作数分成“单元”及 ...

  3. 【shell】条件判断式

    条件判断式的表示格式: 文件判断式: [root@andon ~]# [ -e /root/1 ] && echo yes || echo no #注意[]里面的空格,第一个命令为真打 ...

  4. 第十三章、学习 Shell Scripts 条件判断式

    利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 i ...

  5. 第十三章、学习 Shell Scripts 善用判断式

    善用判断式 利用 test 命令的测试功能 我要检查 /dmtsai 是否存在时,使用: [root@www ~]# test -e /dmtsai [root@www ~]# test -e /dm ...

  6. Linux学习十八之、善用判断式

    原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_3.php 善用判断式 在第十一章中,我们提到过 $? 这个变量所 ...

  7. shell编程 条件判断式----利用 case ..... esac 判断

    条件判断式----利用 case ..... esac 判断 case  $变量名称 in   <==关键词为 case ,还有变量前有钱字号 "第一个变量内容")   &l ...

  8. shell编程 条件判断式----利用 if .... then ----多重

    条件判断式----利用 if .... then ----多重 在同一个数据的判断中,如果该数据需要进行多种不同的判断时,应该怎么作?举例来说,上面的 sh06.sh 脚本中,我们只要进行一次 $yn ...

  9. Linux学习-善用判断式

    利用 test 指令的测试功能 要检测系统上面某些文件或者是相关的属性时,利用 test 这个指令来工作真是好用得不 得了, 举例来说,我要检查 /dmtsai 是否存在时,使用: [dmtsai@s ...

随机推荐

  1. centos7-硬盘坏道检测

    #检测坏道命令,结果输出到 /home/badblocks.log badblock -s -v -o /home/badblocks.log /dev/sdb1 [注]:硬盘损坏程度不同,block ...

  2. 检验Xcode是否被改动过的简单方法,不妨试试!!!

    检验Xcode是否被改动过的简单方法,不妨试试!!!       在终端系统上运行以下命令启用检测: spctl --assess --verbose /Applications/Xcode.app  ...

  3. Number 和 parseInt 区别

    把字符串 转换成 数字的时候, Number 有点不靠谱, 因为会对 '' 和 null 转换成0, parseInt 相对靠谱些; 判断是否是数值时, isNaN 对于字符串'2'的判断是数字, 对 ...

  4. BZOJ2303 APIO2011方格染色

    这题太神了 首先我们可以发现只有当i和j都是偶数时a[1][1]^a[1][j]^a[i][1]^a[i][j]=1才满足情况,其它时都为0 所以我们可以先把i和j都为偶数的地方^1变为0 下面才是最 ...

  5. Ubuntu14.04 安装中文输入法

    1 安装fcitx   sudo  apt-get install fcitx-table-py    这里py是拼音的意思,安装关依赖库和框架都会自动安装  2 把Ubuntu的系统环境改成中文  ...

  6. bzoj 2693

    收获: 1.积性函数的积也是积性函数,基本的积性函数:常数函数,正比例函数,欧拉函数,Mobius函数,积性函数一般都知道表达式,所以一般都可以在线性筛时搞定. 2.遇到整除求和时,这个东西就已经是最 ...

  7. noip200204过河卒

    如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点.例如上图 C ...

  8. yaf学习

    <?php 安装 phpize ./configure --with-php-config=/usr/local/php/bin/php-config 路由类 final Yaf_Router ...

  9. MySQL -- 性能优化的最佳20+条经验

    FROM:http://www.cnblogs.com/shlhm/p/3235848.html ,学习学习~ 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库 ...

  10. 拉普拉斯(Laplace)分布

    Laplace分布的概率密度函数的形式是这样的: $p(x) = \frac{1}{2 \lambda} e^{-\frac{\vert x –\mu \vert}{\lambda}}$   一般$\ ...