博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL - C++ 11的Lambda表达式(下)
阅读量:6153 次
发布时间:2019-06-21

本文共 4073 字,大约阅读时间需要 13 分钟。

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

 

转载于:https://www.cnblogs.com/davidgu/p/4825827.html

你可能感兴趣的文章
转:Vue keep-alive实践总结
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
centos 下安装g++
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
jquery用法大全
查看>>
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>