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. 洛谷 P1115 最大子段和

    P1115 最大子段和 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N ...

  2. 「WC2010」重建计划(长链剖分/点分治)

    「WC2010」重建计划(长链剖分/点分治) 题目描述 有一棵大小为 \(n\) 的树,给定 \(L, R\) ,要求找到一条长度在 \([L, R]\) 的路径,并且路径上边权的平均值最大 \(1 ...

  3. [BZOJ4196][NOI2015]软件包管理器(树链剖分)

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2166  Solved: 1253[Submit][Sta ...

  4. 【KM】BZOJ1937 [Shoi2004]Mst 最小生成树

    这道题拖了好久因为懒,结果1A了,惊讶∑( 口 || [题目大意] 给定一张n个顶点m条边的有权无向图.现要修改各边边权,使得给出n-1条边是这张图的最小生成树,代价为变化量的绝对值.求最小代价之和. ...

  5. [YC703]ゴミ拾い Easy

    [YC703]ゴミ拾い Easy 题目大意: 二维平面内有\(n(n\le3\times10^5)\)个人和\(n\)个物品,第\(i\)个人在\((a_i,0)\)上,第\(i\)个物品在\((x_ ...

  6. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  7. 一文了解JVM全部垃圾回收器,从Serial到ZGC

    <对象搜索算法与回收算法>介绍了垃圾回收的基础算法,相当于垃圾回收的方法论.接下来就详细看看垃圾回收的具体实现. 上文提到过现代的商用虚拟机的都是采用分代收集的,不同的区域用不同的收集器. ...

  8. 三周学会小程序第四讲:Heroku 绑定 Github 自动部署

    这一讲是根据读者的反馈补充的一个讲解,好多读者反应安装 Heroku-cli 遇到问题,或者是操作繁琐,其实上一讲中提到的 Heroku 只是为了免费部署,而安装 Heroku-CLI只是为了部署,所 ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials

    D. Bear and Polynomials 题目连接: http://www.codeforces.com/contest/658/problem/D Description Limak is a ...

  10. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...