For a basic understanding of lambda, please refer to the previous address below:
Http://www.cnblogs.com/davidgu/p/4825625.html
Let's give an example of the STL using lambda for sorting, as follows:
Person.h
#ifndef _domain_models_person_h_#define_domain_models_person_h_#include<iostream>#include<string>#include<deque>using namespacestd;classPerson {friend Ostream&operator<< (ostream& S,Constperson&p);Private: stringfn//First name stringln//Last name intAge ; Public: Person () {} person (Const string& F,Const string&N): FN (f), ln (n) {}stringFirstName ()Const; stringLastName ()Const; intGetage ()Const; voidSetage (inta); Static BOOLSortbyname (Constperson& P1,Constperson&p2); Static BOOLSortbyage (Constperson& P1,Constperson&p2); Static voidSortdequebyname (Deque<person> &persons); Static voidSortdequebyage (Deque<person> &persons); Static voidPrintpersondeques (deque<person>persons);};#endif
Person.cpp
#include <algorithm>#include"Person.h"inlinestringPerson::firstname ()Const { returnfn;} InlinestringPerson::lastname ()Const { returnLn;} InlineintPerson::getage ()Const{ returnAge ;}voidPerson::setage (inta) { Age=A;}/*binary Function predicate:*-Returns whether a person was less than another person*/BOOLPerson::sortbyname (Constperson& P1,Constperson&p2) { /*A person was less than another person *-if the last name was less *-if the last name is equal and the first n Ame is less*/ returnP1.lastname () <p2.lastname () | |(P1.lastname ()= = P2.lastname () &&P1.firstname ()<p2.firstname ());}//another binary predicateBOOLPerson::sortbyage (Constperson& P1,Constperson&p2) { returnP1.getage () <p2.getage ();}voidPerson::sortdequebyname (Deque<person> &persons) { //Sort ElementsSort (Persons.begin (), Persons.end (),//RangePerson::sortbyname);//sort criterion}voidPerson::sortdequebyage (Deque<person> &persons) { //Sort ElementsSort (Persons.begin (), Persons.end (),//RangePerson::sortbyage);//sort criterion}ostream&operator<< (ostream& S,Constperson&p) {s<<"["<< P.lastname () <<", "<< P.firstname () <<", "<< p.getage () <<"]"; returns;}voidPerson::p Rintpersondeques (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 namespacestd;voidLambdatest::sortbylambda () {//Create some personsPerson 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 (2); P5.setage ( A); P6.setage ( *); P7.setage ( the); //Insert person into collection CollDeque<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::p Rintpersondeques (coll); //sort Persons According to LastName (and FirstName)Sort (Coll.begin (), Coll.end (),//Range[](Constperson& P1,Constperson& p2) {//sort criterion returnP1.lastname () <p2.lastname () | |(P1.lastname ()= = P2.lastname () &&P1.firstname ()<p2.firstname ()); }); cout<<"persons after sort by name:"<<Endl; Person::p Rintpersondeques (coll); //sort Persons according to ageSort (Coll.begin (), Coll.end (),//Range[](Constperson& P1,Constperson& p2) {//sort criterion returnP1.getage () <P2.getage (); }); cout<<"persons after sort by:"<<Endl; Person::p Rintpersondeques (coll);}voidLambdatest::run () {Printstart ("Sortbylambda ()"); Sortbylambda (); Printend ("Sortbylambda ()");}
Operation Result:
----------------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:
[Josuttis, Lucas, 2]
[Josuttis, Anica, 18]
[Josuttis, Nicolai, 20]
[Otto, Lucas, 22]
[Josuttis, Ulli, 30]
[Arm, Lucas, 35]
[Holle, Anica, 95]
----------------Sortbylambda (): Run End----------------
Lambda expression for stl-c++ 11 (bottom)