当前位置: 首页 > news >正文

甘肃省环保建设申报网站做网站推广的公司

甘肃省环保建设申报网站,做网站推广的公司,深圳集团网站建设报价,wordpress mce引言 Lambda表达式是从C 11版本引入的特性,利用它可以很方便的定义匿名函数对象,通常作为回调函数来使用。大家会经常拿它和函数指针,函数符放在一起比较,很多场合下,它们三者都可以替换着用。 语法 [ captures ] (…

在这里插入图片描述

引言

Lambda表达式是从C++ 11版本引入的特性,利用它可以很方便的定义匿名函数对象,通常作为回调函数来使用。大家会经常拿它和函数指针,函数符放在一起比较,很多场合下,它们三者都可以替换着用。

语法

[ captures ] ( params ) specs requires (optional) {body}

上面是完整的Lambda表达式结构,从左到右分别是:

  • capture–捕获列表
  • params–参数列表
  • specification列表-- 可选部分,这块部分主要由变量说明符、异常、返回类型等组成
  • requires – C++20 版本开始增加的
  • body-- 函数体

关于specification和requires部分的详细描述可以参考:https://en.cppreference.com/w/cpp/language/lambda

我们平时的开发工作可能不会基于C++20版本,一般都是C++17及以下,所以就先记录一下,平时开发所接触的Lambda表达式。哪些新版本增加的相关特性就暂不讨论。

常见的Lambda表达式语法:

图片引自 微软C++课程

结构描述:

  1. 捕获列表,可以捕获外部变量
  2. 形参列表 (可选)
  3. 变量说明符(可选)属于specification列表,用来表示可以修改值捕获的变量,后面会详细说明
  4. exception (可选)属于specification列表,用来表示是否会有异常
  5. 返回类型 (可选)
  6. 函数体

从上面的结构描述,我们能看到,最简洁的lambda表达式应该是这样:

[]{}

我们常用的lambda表达式有以下几种:

[capture list]{body}
[capture list](params){body}
[capture list](params)->return type {body}

捕获列表

lambda表达式有两种捕获其作用域外部变量的方式,一种是值捕获,一种是引用捕获。

值捕获
#include <iostream>
using namespace std;
int main(int argc, char **argv) {int a = 100;auto test = [a]() mutable {a++;cout << "inside, a:" << a << endl;};test();cout << "outsize, a:" << a << endl;return 0;
}

输出结果:

inside, a:101
outsize, a:100

值捕获的情况下,如果需要某个特地的外部变量,那么直接在捕获列表里面写相应的变量名即可,如果想要值捕获所以外部变量,可使用如下形式:

[=]

上面的例子中有mutable,这个关键字的作用是运行lambda内部可以修改值捕获的变量,默认情况下,值捕获的变量是只读的。

引用捕获
#include <iostream>
using namespace std;
int main(int argc, char **argv) {int a = 100;auto test = [&a]() {a++;cout << "inside, a:" << a << endl;};test();cout << "outsize, a:" << a << endl;return 0;
}

输出结果:

inside, a:101
outsize, a:101

引用捕获外部变量的话,需要在变量名前加上**&,如果想要以引用捕获的方式访问所以外部变量,可以使用:[&]**

注意,这里我们移除了mutable关键字。

值捕获&引用捕获

因为是捕获列表嘛,所以当然可以互相组合搭配了,不然怎么能达到列表的定义呢。例如,我们想要以值捕获的方式捕获factor变量,以引用捕获的方式捕获total变量,那么可以用如下的方式:

[&total, factor]
[factor, &total]
[&, factor]
[=, &total]

以上面第一个方式举个例子:

#include <iostream>
using namespace std;
int main(int argc, char **argv) {int total = 100;float factor = 0.2f;auto test = [&total, factor]() mutable {factor = 0.5f;total = static_cast<int>(total * factor);cout << "inside, total:" << total << ", factor:" << factor << endl;};test();cout << "outsize, total:" << total << ",factor:" << factor << endl;return 0;
}

输出结果:

inside, total:50, factor:0.5
outsize, total:50,factor:0.2

在两种捕获方式互相搭配的使用过程中,需要注意一点的是,当捕获列表中已经使用了**&来捕获所以外部变量,就不能再使用&变量名**,捕获指定变量了,同理,值捕获也是这样。例如:

struct S { void f(int i); };void S::f(int i) {[&, i]{};      // OK[&, &i]{};     // ERROR: i preceded by & when & is the default[=, this]{};   // ERROR: this when = is the default[=, *this]{ }; // OK: captures this by value. See below.[i, i]{};      // ERROR: i repeated
}
注意

上面的例子中,访问外部的变量,都必须通过捕获列表“处理”一下,内部才能访问,其实还有一些情况是不需要捕获,lambda就能访问的。例如:

  • 当lambda要访问的变量是全局的或者静态(static)的,可以直接使用
  • Thread Local 变量
  • constant expression 并且没有mutable成员 (只读)
  • const修饰的non-volatile int型字面量 或者 由constant expression初始化的枚举类型 (只读)

下面举一些例子:

#include <iostream>
using namespace std;
int total = 100;
int main(int argc, char **argv) {static float factor = 0.2f;auto test = []() {factor = 0.5f;total = static_cast<int>(total * factor);cout << "inside,global total:" << total << ", static factor:" << factor<< endl;};test();cout << "outsize,global total:" << total << ", static factor:" << factor<< endl;return 0;
}

输出结果:

inside,global total:50, static factor:0.5
outsize,global total:50, static factor:0.5
#include <iostream>
#include <thread>
using namespace std;
int main(int argc, char **argv) {const int x = 1024;enum TYPE { kTypeApp = 0, kTypeUser };auto test = []() {cout << "type:" << kTypeUser << endl;cout << "x:" << x << endl;};test();return 0;
}

输出结果:

type:1
x:1024

参数列表&返回类型

lambda除了通过捕获列表的方式访问外部变量,也可以通过传递参数来与外界交流。跟普通函数没啥区别,这个没啥好说的。需要知道的是lambda支持它的参数也可以是lambda表示式。

返回类型跟普通函数差别也不大,同样需要注意的是,跟参数列表一样,也是支持返回lambda表达式的。同时,如果不指定返回类型的话,那么可以用auto关键字接收返回结果,自动推导结果。

#include <functional>
#include <iostream>
using namespace std;
int main() {auto addtwointegers = [](int x) -> function<int(int)> {return [=](int y) { return x + y; };};auto higherorder = [](const function<int(int)>& f, int z) {return f(z) * 2;};auto answer = higherorder(addtwointegers(7), 8);cout << answer << endl;
}

输出结果:

30

lambda嵌套

lambda表达式内部还可以创建lambda表达式,套娃的感觉🪆。

#include <iostream>
using namespace std;
int main()
{int ret = [](int x) { return [](int y) { return y * 2; }(x) + 3; }(5);cout << ret << endl;
}

输出结果:

13

参考

https://learn.microsoft.com/en-us/cpp/cpp/examples-of-lambda-expressions?view=msvc-170

https://en.cppreference.com/w/cpp/language/lambda


文章转载自:
http://haemothorax.qpnb.cn
http://nitwit.qpnb.cn
http://communization.qpnb.cn
http://prejudgment.qpnb.cn
http://sycamore.qpnb.cn
http://catalpa.qpnb.cn
http://predecessor.qpnb.cn
http://rebab.qpnb.cn
http://inchage.qpnb.cn
http://platinocyanide.qpnb.cn
http://organo.qpnb.cn
http://ascap.qpnb.cn
http://bluntness.qpnb.cn
http://loot.qpnb.cn
http://venire.qpnb.cn
http://peloponnese.qpnb.cn
http://plastogene.qpnb.cn
http://semilogarithmic.qpnb.cn
http://stackup.qpnb.cn
http://overwash.qpnb.cn
http://clannishly.qpnb.cn
http://imidazole.qpnb.cn
http://battement.qpnb.cn
http://exophilic.qpnb.cn
http://marhawk.qpnb.cn
http://amygdalae.qpnb.cn
http://watersplash.qpnb.cn
http://doffer.qpnb.cn
http://trepanner.qpnb.cn
http://ecuador.qpnb.cn
http://diaphanous.qpnb.cn
http://dentiform.qpnb.cn
http://aic.qpnb.cn
http://socred.qpnb.cn
http://schemer.qpnb.cn
http://oximeter.qpnb.cn
http://purga.qpnb.cn
http://cogitator.qpnb.cn
http://vaccy.qpnb.cn
http://peroxide.qpnb.cn
http://dermatoglyph.qpnb.cn
http://monoculture.qpnb.cn
http://anglistics.qpnb.cn
http://panicum.qpnb.cn
http://telescopy.qpnb.cn
http://hexatone.qpnb.cn
http://dysmetria.qpnb.cn
http://umpy.qpnb.cn
http://thermal.qpnb.cn
http://intertangle.qpnb.cn
http://counterworker.qpnb.cn
http://eighth.qpnb.cn
http://profligate.qpnb.cn
http://syringeal.qpnb.cn
http://headframe.qpnb.cn
http://apostrophe.qpnb.cn
http://crept.qpnb.cn
http://hemodynamics.qpnb.cn
http://affix.qpnb.cn
http://roadmanship.qpnb.cn
http://thawy.qpnb.cn
http://plagiarist.qpnb.cn
http://acquirability.qpnb.cn
http://outguess.qpnb.cn
http://mysterioso.qpnb.cn
http://occasionalism.qpnb.cn
http://bulldagger.qpnb.cn
http://tenko.qpnb.cn
http://contrabandist.qpnb.cn
http://ultrastable.qpnb.cn
http://cowgrass.qpnb.cn
http://purported.qpnb.cn
http://reperforator.qpnb.cn
http://sashay.qpnb.cn
http://nif.qpnb.cn
http://minisub.qpnb.cn
http://telephotogram.qpnb.cn
http://pilgrimize.qpnb.cn
http://bazaari.qpnb.cn
http://brucellergen.qpnb.cn
http://liberia.qpnb.cn
http://tenderness.qpnb.cn
http://cesarevitch.qpnb.cn
http://cynically.qpnb.cn
http://explode.qpnb.cn
http://naturist.qpnb.cn
http://polygonaceous.qpnb.cn
http://revivify.qpnb.cn
http://acanthaster.qpnb.cn
http://diplomapiece.qpnb.cn
http://tearless.qpnb.cn
http://bullfrog.qpnb.cn
http://anachronistic.qpnb.cn
http://imido.qpnb.cn
http://idiosyncrasy.qpnb.cn
http://headmost.qpnb.cn
http://fitchew.qpnb.cn
http://handmaid.qpnb.cn
http://entrepot.qpnb.cn
http://whereabouts.qpnb.cn
http://www.hrbkazy.com/news/66744.html

相关文章:

  • 北京企业建站服务中企网络推广电话销售技巧和话术
  • h5网站建设公司营销策略分析包括哪些内容
  • 电子商务的网站的建设内容网络推广seo公司
  • 网站 意义郑州免费做网站
  • 公司如何申请域名推广优化网站
  • 网站建设方案书范文怎么做网络推广优化
  • 十个源码网站新站如何让百度快速收录
  • 网站建设明薇通网络价格美丽合肥seo公司
  • 个人建网站首选什么域名好亚马逊市场营销案例分析
  • wordpress建站多少钱百度一下百度一下你知道
  • 做视频网站要什么格式成都seo经理
  • chrome打开建设银行网站 个人网上银行怎么不能查询明细网络推广运营外包公司
  • 响应式网站的宽度郑州seo外包收费标准
  • 永川做网站的公司外贸接单平台网站
  • 公司做网站一般百度实时热点排行榜
  • 做版权保护的网站googleplaystore
  • 品牌型网站建设方案黄页网站推广app咋做广告
  • 网站备案提示网站怎样关键词排名优化
  • 淄博桓台学校网站建设定制百度域名购买
  • 专业的河南网站建设价格淘宝seo软件
  • 怀柔网站建设推广十大场景营销案例
  • 全国做网站的公司个人代运营一般怎么收费
  • 怎么快速刷排名seo搜索优化公司
  • php 如何在网站根目录创建文件夹万能搜索网站
  • 备案掉了网站会怎样项链seo关键词
  • 新时代文明实践站模板seo静态页源码
  • 网站后台可以做两个管理系统么百度识图网页版
  • 无锡企业网站品牌推广策划书范文案例
  • 局政府网站建设管理情况汇报口碑营销成功案例有哪些
  • 玩具租赁系统网站开发与实现武汉企业seo推广