码迷,mamicode.com
首页 > 编程语言 > 详细

STL - C++ 11的Lambda表达式(下)

时间:2015-09-21 14:05:11      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

关于lambda的基础知识,请参考上一篇的地址如下:

http://www.cnblogs.com/davidgu/p/4825625.html

 

我们再举个STL使用Lambda来进行排序的例子,如下:

Person.h

#ifndef _Domain_Models_Person_H_
#define _Domain_Models_Person_H_

#include <iostream>
#include <string>
#include <deque>

using namespace std;

class Person 
{
    friend ostream& operator<< (ostream& s, const Person& p);
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);
    static void printPersonDeques(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
}

ostream& operator<< (ostream& s, const Person& p)
{
    s << "[" << p.lastname() << ", " << p.firstname() << ", " << p.getAge() << "]";
    return s;
}

void Person::printPersonDeques(deque<Person> persons)
{
    deque<Person>::iterator pos;
    for (pos = persons.begin(); pos != persons.end(); ++pos)
    {
        cout << *pos << endl;
    }
}

 

LambdaTest.cpp

#include <algorithm>
#include <deque>
#include <iostream>
#include "LambdaTest.h"
#include "../../Core/ContainerUtil.h"

using namespace std;

void LambdaTest::sortByLambda()
{
    // 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);

    cout << "persons before sort:" << endl;
    Person::printPersonDeques(coll);

    // sort Persons according to lastname (and firstname)
    sort(coll.begin(), coll.end(),                // range
        [](const Person& p1, const Person& p2) { // sort criterion
        return p1.lastname()<p2.lastname() ||
            (p1.lastname() == p2.lastname() &&
            p1.firstname()<p2.firstname());
    });

    cout << "persons after sort by name:" << endl;
    Person::printPersonDeques(coll);

    // sort Persons according to age
    sort(coll.begin(), coll.end(),                // range
        [](const Person& p1, const Person& p2) { // sort criterion
        return p1.getAge() < p2.getAge();
    });

    cout << "persons after sort by age:" << endl;
    Person::printPersonDeques(coll);
}

void LambdaTest::run()
{
    printStart("sortByLambda()");
    sortByLambda();
    printEnd("sortByLambda()");
}

 

运行结果:

---------------- sortByLambda(): Run Start ----------------
persons before sort:
[josuttis, nicolai, 20]
[josuttis, ulli, 30]
[josuttis, anica, 18]
[josuttis, lucas, 2]
[otto, lucas, 22]
[arm, lucas, 35]
[holle, anica, 95]
persons after sort by name:
[arm, lucas, 35]
[holle, anica, 95]
[josuttis, anica, 18]
[josuttis, lucas, 2]
[josuttis, nicolai, 20]
[josuttis, ulli, 30]
[otto, lucas, 22]
persons after sort by age:
[josuttis, lucas, 2]
[josuttis, anica, 18]
[josuttis, nicolai, 20]
[otto, lucas, 22]
[josuttis, ulli, 30]
[arm, lucas, 35]
[holle, anica, 95]
---------------- sortByLambda(): Run End ----------------

 

STL - C++ 11的Lambda表达式(下)

标签:

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

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