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. 表视图(UITableView)与表视图控制器(UITableViewController)

    表视图(UITableView)与表视图控制器(UITableViewController)其实是一回事. 表视图控制器是一种只能显示表视图的标准视图控制器,可在表视图占据整个视图时使用这种控制器.虽 ...

  2. 【MPI】并行求和

    比较简单的并行求和 读入还是串行的 而且无法处理线程数无法整除数据总长度的情况 主要用到了MPI_Bcast MPI_Scatter MPI_Reduce typedef long long __in ...

  3. 【四边形不等式】HDU3506-Monkey Party

    [题目大意] 香蕉森林里一群猴子(n<=1000)围成一圈开会,会长给他们互相介绍,每个猴子需要时间a[i].每次只能介绍相邻的两只猴子x和y认识,同时x所有认识的猴子和y所有认识的猴子也就相互 ...

  4. [NC13B]贝伦卡斯泰露/[51Nod1400]序列分解

    [NC13B]贝伦卡斯泰露/[51Nod1400]序列分解 题目大意: 给定\(A_{1\sim n}(n\le40)\),问是否能将\(A\)分解成两个相同的子序列? 思路: 折半搜索.时间复杂度\ ...

  5. [MVC4]Data Annotations Extensions:无法使用EmailAddress等验证特性的解决方法

    本文地址:http://www.cnblogs.com/egger/p/3404159.html  欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 数据注解(Data Annotations) Web应用 ...

  6. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...

  7. bzoj 1143: [CTSC2008]祭祀river / 2718: [Violet 4]毕业旅行 -- 二分图匹配

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MB Description 在遥远的东方,有一个神秘的民族,自称Y族.他们 ...

  8. UVA 10531 Maze Statistics 迷宫统计 迷宫插头DP 四联通 概率

    题意: 有一个N*M的图,每个格子有独立概率p变成障碍物.你要从迷宫左上角走到迷宫右下角.求每个格子成为一个有解迷宫中的障碍物的概率.N <= 5,M <= 6 分析: 这真是一道好题,网 ...

  9. 三、python的数据类型 列表、元组、字典

    1.list 列表 列表是由一序列特定顺序排列的元素组成的.可以把字符串,数字,字典等都可以任何东西加入到列表中,列表中的元素之间没有任何关系.列表也是自带下标的,默认也还是从0开始. List常用的 ...

  10. DotNet.Utilities工具类

    来源:http://git.oschina.net/kuiyu/dotnetcodes/tree/master/DotNet.Utilities