STL - Predicate - Binary Predicate(双参判断式)
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(双参判断式)的更多相关文章
- STL - 判断式(Predicate) - 单参判断式(Unary Predicate)
Predicate是一种特殊的辅助函数,它会返回Boolean,常常被用来作为排序或者查找准则. Predicate会有1个或者2个操作数. Unary Predicate(单参判断式) 例子: 我们 ...
- 【重点】Shell入门教程:流程控制(3)条件判断式的真假值
之前曾提到,在Bash中什么是真什么是假,是以命令的结束状态是否为0来做判断.传回0,即为真:传回非0,即为假. 在Bash中,这种可以影响程序流程的式子,称为条件判断式.判断式的操作数分成“单元”及 ...
- 【shell】条件判断式
条件判断式的表示格式: 文件判断式: [root@andon ~]# [ -e /root/1 ] && echo yes || echo no #注意[]里面的空格,第一个命令为真打 ...
- 第十三章、学习 Shell Scripts 条件判断式
利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 i ...
- 第十三章、学习 Shell Scripts 善用判断式
善用判断式 利用 test 命令的测试功能 我要检查 /dmtsai 是否存在时,使用: [root@www ~]# test -e /dmtsai [root@www ~]# test -e /dm ...
- Linux学习十八之、善用判断式
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_3.php 善用判断式 在第十一章中,我们提到过 $? 这个变量所 ...
- shell编程 条件判断式----利用 case ..... esac 判断
条件判断式----利用 case ..... esac 判断 case $变量名称 in <==关键词为 case ,还有变量前有钱字号 "第一个变量内容") &l ...
- shell编程 条件判断式----利用 if .... then ----多重
条件判断式----利用 if .... then ----多重 在同一个数据的判断中,如果该数据需要进行多种不同的判断时,应该怎么作?举例来说,上面的 sh06.sh 脚本中,我们只要进行一次 $yn ...
- Linux学习-善用判断式
利用 test 指令的测试功能 要检测系统上面某些文件或者是相关的属性时,利用 test 这个指令来工作真是好用得不 得了, 举例来说,我要检查 /dmtsai 是否存在时,使用: [dmtsai@s ...
随机推荐
- 【欧拉函数】BZOJ4173-数学
[题目大意] [思路] 基本是popoqqq大爷的题解,稍微添加了几句自己的注释,方便理解 同理,如果n%k+m%k<k等价于0 =∑([(n+m)/k]-[n/k]-[m/k])×φ(k) … ...
- Java并发(十一):Condition条件
先做总结: 1.为什么使用Condition条件? synchronized配合Object的wait().notify()系列方法可以实现等待/通知模式. Lock提供了条件Condition,对线 ...
- Python入门,以及简单爬取网页文本内容
最近痴迷于Python的逻辑控制,还有爬虫的一方面,原本的目标是拷贝老师上课时U盘的数据.后来发现基础知识掌握的并不是很牢固.便去借了一本Python基础和两本爬虫框架的书.便开始了自己的入坑之旅 言 ...
- Makefile-有三个非常有用的变量。分别是$@,$^,$<代表的意义
$@ 代表目标文件,$^ 代表所有的依赖文件,$< 代表第一个依赖文件. # 这是简化后的Makefilemain:main.o mytool1.o mytool2.o gcc -o $@ $^ ...
- hdu 4918
第一道树的点分治. 感谢: http://blog.csdn.net/u013368721/article/details/40887575 首先,找出原图的重心(最大子树大小最小的点(如果作为根)) ...
- ACM -- 算法小结(七)Phone list解题报告
HDOJ -- Phone list解题报告 问题描述:给出一些电话号码,如果有共同前缀则输出NO,如果没有则输出YES. 解题关键:将电话号码进行字符串排序,相邻的电话号码进行比较 Sa ...
- python开发_counter()
在python的API中,提到了Counter,它具有统计的功能 下面是我做的demo: 1.统计自定义字符串中每个字符出现的次数 2.读取一个文件,把文件中的内容转化为字符串,统计该字符串中每个字符 ...
- linux系统相关、硬件、资源 - 相关命令
分类命令: 1.1.系统 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本 # ...
- Constant-on-time buck-boost regulator converts a positive input to a negative output
Buck regulators find wide application as step-down regulators for converting large positive input vo ...
- java多线程知识汇总(三)如何选择锁?如何加锁
1.锁,保证的是被锁的代码,一次执行完毕才能被其他线程执行,锁保证了一个线程执行过程中不被其他线程打断.以保证数据的准确性. 2.数据的读写过程,是有冲突的,当一个线程正在读数据,另一个线程正在写同一 ...