STL - Predicate - Binary Predicate(双参判断式)

简介: Binary Predicate(双参判断式)的用途是:比较两个参数的特定属性   我们先建一个领域模型类: Person.h #ifndef _Domain_Models_Person_H_ #define _Domain_Models_Person_H_ #include ...

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(20);
    p2.setAge(30);
    p3.setAge(18);
    p4.setAge(2);
    p5.setAge(22);
    p6.setAge(35);
    p7.setAge(95);

    // 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 ----------------

 

目录
相关文章
|
6月前
|
监控 算法 安全
带用集合算法set union讲解
带用集合算法set union讲解
79 0
|
6月前
|
存储
并查集Union-find Sets
并查集Union-find Sets
34 0
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
|
Java Spring
Gataway中的Predicate的使用
说白了,Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。
Gataway中的Predicate的使用
|
算法 容器
常用集合算法 set_intersection() set_union() set_difference()
常用集合算法 set_intersection() set_union() set_difference()
Flow之map操作符
Flow之map操作符
213 0
Flow之map操作符
|
机器学习/深度学习 .NET 索引
LInq之Take Skip TakeWhile SkipWhile Reverse Union Concat 用法
废话不多说,直接上代码,代码有注释!自行运行测试! class Program { static void Main(string[] args) { string[] names = { "郭靖", "李莫愁", "欧阳晓晓", "黄蓉", "黄药师", "郭靖", "黄蓉" }; //Take()方法:用于从一个序列的开头返回指定数量的元素。
1040 0