昨天复习到python的lambda表达式,今天总结一下c++ 中的lambda 表达式中的常用用法,只需几分钟,你将很快的学会它。
lambda 常见的形式 [capture block](parameter){ progress}; 其中() 是可选的。
[capture block] :[] 捕捉,这里一般可空,可写你要捕捉的变量
(parameter):() 这个就是写参数的,是可选的,也就是可写,也可以省略
{progress} :{} 这里写你要处理的过程,就是平时写函数处理过程
简单的lambda:
//最简单的lambda
auto basiclambda = [] {cout << "first lambda test" << endl; };
basiclambda();
输出:first lambda test
简单的解析:
这里 我们没有写对应的参数,{}只是处理了一个输出函数,它如同一个函数指针,auto会自动接收,然后调用就可以输出了
带参数的lambda:
//带参数的
auto parameterslambda = [](int value) {cout << "the value is " << value << endl; };
parameterslambda(2);
输出:the value is 2
带返回值的写法:
//待返回值的写法
auto returninglambda = [](int a, int b) ->int { return a + b; };
//省略写法
auto returninglambda2 = [](int a, int b) {return a + b; };
int return1 = returninglambda(2, 4);
int return2 = returninglambda2(2,4);
cout << "return1 is : " << return1 << endl << "return2 is : " << return2 << endl;
输出:
return1 is : 6
return2 is : 6
扑捉域的使用:
//扑捉域的使用
double data = 1.23;
auto capturinglambda = [data] {cout << "the data is " << data << endl; };
capturinglambda();
cout << "data is : " << data << endl;
输出:
the data is 1.23
data is : 1.23
lambda表达式扑捉的是const, 不能进行修改 可以添加关键字 mutable 这样就可以改变data变量(在函数处理中变化,在函数外是不改变的),
auto capturinglambda2 = [data]()mutable {data *= 2; cout << "the data is " << data << endl; };
capturinglambda2();
cout << "data is : " << data << endl;
输出:
the data is 2.46
data is : 1.23
用引用符 &:
//或者才用引用
auto capturinglambda3 = [&data] {data *= 2; cout << "the data is " << data << endl; };
capturinglambda3();
cout << "data is : " << data << endl;
输出:
the data is 2.46
data is : 2.46
这里就发现可以改变了值,已上都是简单的使用,lambda经常配合泛型编程,下面来看看吧
泛型 lambda 表达式
vector<int> ints{ 11,55,101,200 };
vector<double> doubles{11.1,55.5,200.2};
auto isGreaterTan100 = [](auto i) {return i > 100; };
auto it1 = find_if(ints.cbegin(), ints.cend(), isGreaterTan100);
if (it1 != ints.cend())
{
cout << "found the value is " << *it1 << endl;
}
auto it2 = find_if(doubles.cbegin(), doubles.cend(), isGreaterTan100);
if (it2 != doubles.cend())
{
cout << "found the value is " << *it2 << endl;
}
输出:
found the value is 101
found the value is 200.2
lambda 捕捉表达式
auto myPtr =make_unique<double>(3.14159);
auto mylambda = [p = std::move(myPtr)]{ cout << *p; };
mylambda();
输出:3.14159
这里捕捉可以用 参数 = 捕捉对象 来写,这个捕捉的是智能指针,不太了解这部分的小伙伴们,可以查看相关资料
lambda 表达式子作为返回值:
定义函数
auto multiplyBy2Lambda(int x)
{
return [x] {return x * 2; };
}
调用:
//将 lambda 表达式子作为返回值
auto fn = multiplyBy2Lambda(5);
cout << fn() << endl;
输出: 10
将 lambda 表达式用作参数
函数定义:
void testCallBack(const vector<int> & vec,
const function<bool(int)>& callback)
{
for (const auto &i :vec )
{
if (!callback(i))
{
break;
}
cout << i << " ";
}
cout << endl;
}
调用:
//将 lambda 表达式用作参数
vector<int> vec{1,2,3,4,5,6,7,8,9};
testCallBack(vec, [](int i) {return i < 6; });
输出:1 2 3 4 5
这个使用了function的功能,在#include <functional> 中,其实有点像c#中的委托
简单的STL使用
// count_if
int value = 3;
int cnt = count_if(vec.cbegin(), vec.cend(),
[value](int i) {return i < value; });
cout << "Found " << cnt << " value >" << value << endl;
//generate
vector<int> vec2(10);
int value2 = 1;
generate(vec2.begin(), vec2.end(), [&value2] {value2 *= 2; return value2; });
for (const auto & i : vec2)
{
cout << i << " ";
}
cout << endl;
输出:
Found 2 value >3
2 4 8 16 32 64 128 256 512 1024
整个例子:
// lambdaEx.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <functional>
using namespace std;
auto multiplyBy2Lambda(int x)
{
return [x] {return x * 2; };
}
void testCallBack(const vector<int> & vec,
const function<bool(int)>& callback)
{
for (const auto &i :vec )
{
if (!callback(i))
{
break;
}
cout << i << " ";
}
cout << endl;
}
int main()
{
//最简单的lambda
auto basiclambda = [] {cout << "first lambda test" << endl; };
basiclambda();
//带参数的
auto parameterslambda = [](int value) {cout << "the value is " << value << endl; };
parameterslambda(2);
//待返回值的写法
auto returninglambda = [](int a, int b) ->int { return a + b; };
//省略写法
auto returninglambda2 = [](int a, int b) {return a + b; };
int return1 = returninglambda(2, 4);
int return2 = returninglambda2(2,4);
cout << "return1 is : " << return1 << endl << "return2 is : " << return2 << endl;
//扑捉域的使用
double data = 1.23;
auto capturinglambda = [data] {cout << "the data is " << data << endl; };
capturinglambda();
cout << "data is : " << data << endl;
//lambda表达式扑捉的是const, 不能进行修改 想修改请看下面
auto capturinglambda2 = [data]()mutable {data *= 2; cout << "the data is " << data << endl; };
capturinglambda2();
cout << "data is : " << data << endl;
//或者才用引用
auto capturinglambda3 = [&data] {data *= 2; cout << "the data is " << data << endl; };
capturinglambda3();
cout << "data is : " << data << endl;
//泛型 lambda 表达式
vector<int> ints{ 11,55,101,200 };
vector<double> doubles{11.1,55.5,200.2};
auto isGreaterTan100 = [](auto i) {return i > 100; };
auto it1 = find_if(ints.cbegin(), ints.cend(), isGreaterTan100);
if (it1 != ints.cend())
{
cout << "found the value is " << *it1 << endl;
}
auto it2 = find_if(doubles.cbegin(), doubles.cend(), isGreaterTan100);
if (it2 != doubles.cend())
{
cout << "found the value is " << *it2 << endl;
}
//lambda 捕捉表达式
auto myPtr =make_unique<double>(3.14159);
auto mylambda = [p = std::move(myPtr)]{ cout << *p; };
mylambda();
cout << endl;
//将 lambda 表达式子作为返回值
auto fn = multiplyBy2Lambda(5);
cout << fn() << endl;
//将 lambda 表达式用作参数
vector<int> vec{1,2,3,4,5,6,7,8,9};
testCallBack(vec, [](int i) {return i < 6; });
//简单的STL使用
// count_if
int value = 3;
int cnt = count_if(vec.cbegin(), vec.cend(),
[value](int i) {return i < value; });
cout << "Found " << cnt << " value >" << value << endl;
//generate
vector<int> vec2(10);
int value2 = 1;
generate(vec2.begin(), vec2.end(), [&value2] {value2 *= 2; return value2; });
for (const auto & i : vec2)
{
cout << i << " ";
}
cout << endl;
return 0;
}
喜欢的话,请关注我的博客,一起加油。