C++11 Lambda表达式(匿名函数)
发布日期:2021-06-28 22:10:12
浏览次数:2
分类:技术文章
本文共 2717 字,大约阅读时间需要 9 分钟。
语法 序号[ 捕获列表 ] ( 形参数列表 ) mutable(可选) throw()(异常属性) -> 返回值类型 { 函数体 } (1)[ capture-list ] ( params ) -> ret { body } (2)[ capture-list ] ( params ) { body } (3)[ capture-list ] { body } (4)(1)为完整的形式,包含变量捕获列表、形参列表、可变属性(可选)和返回值类型等。(2)省略了mutable,表示Lambda不能修改捕获的变量。(3)Lambda的返回值类型如果可以由函数体中的实际返回值推导出,可以省略。(4)如果没有形参,可以省略圆括号。mutable 修饰符说明 lambda 表达式体内的代码能够改动被捕获的变量。而且能够訪问被捕获对象的 non-const 方法。exception 说明 lambda 表达式是否抛出异常(noexcept)。以及抛出何种异常,类似于void f()throw(X, Y)。另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表。详细解释例如以下:[a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。[this] 以值的方式捕获 this 指针。[&] 以引用的方式捕获全部的外部自己主动变量。[=] 以值的方式捕获全部的外部自己主动变量。[] 不捕获外部的不论什么变量。例子 int x = 10; int y = 3; int z ; z = [=]()mutable throw() -> int { int n = x + y; x = y ; y = n; return n;}(); cout<<z<<endl; cout<<"x:"<<x<<"\t"<<"y:"<<y<<endl;运行结果为:13x: 10 y: 3因为是以值传递的方式访问x,y所以x,y的值并没有发生改变#include <iostream>using namespace std;int main(){ int n = [] (int x, int y) { return x + y; }(5, 4); cout << n << endl;}运行结果为:9通过这个例子我们可以看出,通过“函数体”后面的‘()’传入参数。接下来这个例子可以看出,可以像调用函数一样使用lambda表达式,但是感觉这种方式和普通函数的定义与调用就差不多了,这里只是学习使用方式而已。#include <iostream>using namespace std;int main(){ auto f = [] (int x, int y) { return x + y; }; cout << f(21, 12) << endl;}复制代码运行结果为:33#include <vector>#include <iostream>#include <algorithm>#include <functional>int main(){ std::vector<int> c { 1,2,3,4,5,6,7 }; int x = 5; c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end()); std::cout << "c: "; for (auto i: c) { std::cout << i << ' '; } std::cout << '\n'; // the type of a closure cannot be named, but can be inferred with auto auto func1 = [](int i) { return i+4; }; std::cout << "func1: " << func1(6) << '\n'; // like all callable objects, closures can be captured in std::function // (this may incur unnecessary overhead) std::function<int(int)> func2 = [](int i) { return i+4; }; std::cout << "func2: " << func2(6) << '\n'; }//配合boost库#include <boost/function.hpp>typedef boost::function<void(int)> fobject_t;// Now this function may accept functional objectsvoid process_integers(const fobject_t& f);#include <assert.h>#include <deque>int main() { // lambda function with no parameters that does nothing process_integers([](int /*i*/){}); // lambda function that stores a reference std::deque<int> ints; process_integers([&ints](int i){ ints.push_back(i); }); // lambda function that modifies its content std::size_t match_count = 0; process_integers([ints, &match_count](int i) mutable { if (ints.front() == i) { ++ match_count; } ints.pop_front(); }); assert(match_count == 6);}void process_integers(const fobject_t& f) { static const int data[] = {1, 2, 3, 4, 5, 200, 0}; std::for_each(data, data + 6, f);}转载地址:https://blog.csdn.net/yhc166188/article/details/79298035 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月23日 09时05分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
journal of neuroscience:面孔的神经表征与眼动模式相协调
2019-04-28
The Neuroscientist:运动性脑震荡的长期影响
2019-04-28
机器学习实战学习笔记一
2019-04-28
CString.Buffer()的作用
2019-04-28
【一次认识一个市场技术指标】之RSI(相对强弱指标)
2019-04-28
【vn.py】 策略实盘自动交易
2019-04-28
Java之Collections工具类方法使用以及源码分析(三)
2019-04-28
Flink之Java 8
2019-04-28
Flink之修改StreamExecutionEnvironment配置Job
2019-04-28
Why Apache Flink®?
2019-04-28
仿牛客社区项目2.5登录模块———登录退出功能
2019-04-28
仿牛客社区项目3.5——显示评论(普通功能,Controller层帖子-评论-回复)
2019-04-28
仿牛客社区项目3.6——增加评论,同时更新评论数【事务】
2019-04-28
[golang]-go中字符串格式化与fmt包简介
2019-04-28
[Leet-go]-复杂链表的复制
2019-04-28
五分钟上手ECharts图形报表
2019-04-28
权限控制之Spirng Security框架
2019-04-28
android课程表!大厂offer手到擒来,满满干货指导
2019-04-29
android网!2021中级Android开发面试解答,进阶学习资料!
2019-04-29