码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-17 13:20:24      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/davidgu/p/4815882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!