关于lambda的基础知识,请参考上一篇的地址如下:
http://www.cnblogs.com/davidgu/p/4825625.html
我们再举个STL使用Lambda来进行排序的例子,如下:
Person.h
#ifndef _Domain_Models_Person_H_#define _Domain_Models_Person_H_#include#include #include 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 &persons); static void sortDequeByAge(deque &persons); static void printPersonDeques(deque persons);};#endif
Person.cpp
#include#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() &persons){ // sort elements sort(persons.begin(), persons.end(), // range Person::sortByName); // sort criterion}void Person::sortDequeByAge(deque &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 persons){ deque ::iterator pos; for (pos = persons.begin(); pos != persons.end(); ++pos) { cout << *pos << endl; }}
LambdaTest.cpp
#include#include #include #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 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()
运行结果:
---------------- 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 ----------------