STL - C++ 11的Lambda表达式(下)
关于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();
p2.setAge();
p3.setAge();
p4.setAge();
p5.setAge();
p6.setAge();
p7.setAge(); // 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表达式(下)的更多相关文章
- STL - C++ 11的Lambda表达式(上)
Lambda始自C++ 11,是一种在表达式或语句内指定函数行为的定义式. 你可以定义函数行为作为对象,以inline实参的形式传给算法作为predicate(判断式). eg: std:transf ...
- 「C++11」Lambda 表达式
维基百科上面对于 lambda 的引入是如下描述的: 在标准 C++,特别是当使用 C++ 标准程序库算法函数诸如 sort 和 find.用户经常希望能够在算法函数调用的附近定义一个临时的述部函数( ...
- C++11之lambda表达式
lambda表达式源于函数式编程的概念,它可以就地匿名定义目标函数或函数对象,不需要额外写一个命名函数或者函数对象.lambda表达式的类型在C++11中被称为"闭包类型",也可以 ...
- C++11 里lambda表达式的学习
最近看到很多关于C++11的文档,有些是我不怎么用到,所以就略过去了,但是lambda表达式还是比较常用的,其实最开始学习python的时候就觉得lambda这个比较高级,为什么C++这么弱.果然C+ ...
- C++11之lambda表达式解析
什么是Lanmbda? 简短函数,就地书写.常用于向函数(算法)传递函数参数. 语法 Lambda 表达式,[capture](paras)mutable->return type{statem ...
- c++11:lambda表达式的使用
lambda表达式的一般形式: [capture list] (parameter list) -> return type{function body}; 其中,capture list (捕 ...
- 【C++11】 lambda表达式
i.e.int x = 10;int y = 20;int z = [&]{ x = x * x; y = y * y; return x + y;}(); 上面z后面以[]开头的为一个lam ...
- C++11之lambda表达式应用
应用 foreach语句中 #include <time.h> #include <algorithm> using namespace std; void func(int ...
- C++ 11 Lambda表达式
C++11的一大亮点就是引入了Lambda表达式.利用Lambda表达式,可以方便的定义和创建匿名函数.对于C++这门语言来说来说,“Lambda表达式”或“匿名函数”这些概念听起来好像很深奥,但很多 ...
随机推荐
- 【atcoder F - Namori】**
F- Namori http://agc004.contest.atcoder.jp/tasks/agc004_f Time limit : 2sec / Memory limit : 256MB S ...
- 韩梦飞沙-屏幕录像专家 win10 含注册机
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙-屏幕录像专家 win10 含注册机 百度云盘下载地址:https://pan. ...
- 【四边形不等式】noi95- 合并石子
[题目大意] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法,计算出将N堆石子合并成 ...
- 升级到php7和安装拓展(mac centos)
Mac升级到php7 使用homebrew安装php7 brew update #更新源 brew search php #查找源中的php,发现有php7.1版本,安装最新的php7.1 brew ...
- HTML导出Excel数据类型转换样式参考
mso-number-format:"0" NO Decimals mso-number-format:"0/.000" 3 Decimals mso-numb ...
- C# 基于正则表达式的字符串验证
输入的字符串校验,是开发中经常遇到的问题,常用的办法是利用正则表达式进行判断.其特点是简洁有效. 1.正则表达基础知识 正则表达式的教程很多,这里两个基础教程: a.http://www.cnblog ...
- 如何在Ubuntu中用firefox浏览器查看chm文档?
首先下载这插 件:在firefox中点击“工具”->“附加软件”->“扩展”,在firefix扩展网页下搜索“"chmfox" 然后安装,重启后就可以了.
- iframe里面的页面调用父窗口,左右窗口js函数的方法
iframe里面的页面调用父窗口,左右窗口js函数的方法 实现iframe内部页面直接调用该iframe所属父窗口自定义函数的方法. 比如有A窗口,A内有个IFRAME B,B里面的装载的是C页面 ...
- CMSIS-SVD Reference
http://www.keil.com/pack/doc/cmsis/svd/html/modules.html SVD File Schema Levels Device Level Periphe ...
- gdb参考手册
http://www.sourceware.org/gdb/current/onlinedocs/gdb.html