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

广告公司网站建设方案市场调研的方法

广告公司网站建设方案,市场调研的方法,日喀则市住房和城乡建设局网站,烟台专业网站推广文章首发公众号:iDoitnow 为什么引入Lambda Lambda表达式是一个可以内联在我们代码中的函数,我们可以将他传递给另外一个函数。在没有引入Lambda表达式之前,当我们遇到需要对多个数据,按照同一规则进行操作的时候,创建…

文章首发公众号:iDoitnow

为什么引入Lambda

Lambda表达式是一个可以内联在我们代码中的函数,我们可以将他传递给另外一个函数。在没有引入Lambda表达式之前,当我们遇到需要对多个数据,按照同一规则进行操作的时候,创建机动函数会更简单,但是必须在其他地方定义好该函数,然后再使用它,有时候两者之间可能距离离的很远,想要了解该函数内部操作的原理,需要翻阅多页源代码找它的定义,这样使得代码的可读性大打折扣。这就是Lambda表达式出现的主要原因。

基本的Lambda语法

lambda表达式是一个匿名函数。其可以捕获作用域中的变量。其在C++11中常用的语法如下:

[captures](params){body}
[captures]{body}  //省略了形参列表,函数不接收实参,如同形参列表为()
[captures](params)->return-type{body}  //return-type为返回类型//eg:
//捕获列表     参数列表           函数体[ ]   (int a, int b)  { return a < b;}
//捕获列表     参数列表      返回类型          函数体[ ]   (double x)  -> double   {int y = x; return x - y;}
/*注:Lambda函数没有声明返回类型,其返回类型相当于使用decltyp根据返回值推断得到的。如果函数体内不包含返回语句,则推断出返回类型为void。但是仅当函数体完全由一条返回语句组成时,自动类型推断才管用否则,需要使用新增的返回类型后置语法*/

:Lambda表达式自动推断返回值类型:有些编译器,像GCC,即使你有多于一个返回语句也不可以自动推断成功,但标准不保证这一点,因此为了防止意外的发生,当函数体内返回语句不止一条的时候,建议使用返回类型后置语法】

其中captures为捕获,它一个包含0个或者多个捕获符的用逗号分隔的列表。params为参数列表,body为函数体。默认的捕获符只有:

  • &:采用引用隐式捕获的方式使用自动变量

  • =:采用复制隐式捕获的方式使用自动变量

    []        // 默认不捕获
    [&]       // 默认以引用捕获(意味着在函数体内可以按照引用的方式使用Lambda表达式所在范围内所有可见的局部变量)
    [&, i]    // 以引用捕获(同上),但i以值捕获
    [=]       // 默认以复制捕获(意味着在函数体内可以按照值传递的方式使用Lambda表达式所在范围内所有可见的局部变量)
    [=, &i]   // 以复制捕获(同上),但 i 以引用捕获
    

下面我们通过C++ Primer Plus中的一个例子进一步理解Lambda表达式:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
const long Size = 390000L;
int main()
{using std::cout;std::vector<int> numbers(Size);std::srand(std::time(0));std::generate(numbers.begin(), numbers.end(), std::rand);cout << "Sample size = " << Size << '\n';// using lambdasint count3 = std::count_if(numbers.begin(), numbers.end(),[](int x){return x % 3 == 0;});cout << "Count of numbers divisible by 3: " << count3 << '\n';int count13 = 0;std::for_each(numbers.begin(), numbers.end(),[&count13](int x){count13 += x % 13 == 0;});cout << "Count of numbers divisible by 13: " << count13 << '\n';// using a single lambdacount3 = count13 = 0;std::for_each(numbers.begin(), numbers.end(),[&](int x){count3 += x % 3 == 0; count13 += x % 13 == 0;});cout << "Count of numbers divisible by 3: " << count3 << '\n';cout << "Count of numbers divisible by 13: " << count13 << '\n';return 0;
}

程序输出结果:

Sample size = 390000
Count of numbers divisible by 3: 130274
Count of numbers divisible by 13: 30009
Count of numbers divisible by 3: 130274
Count of numbers divisible by 13: 30009

Lambda与STL的结合

当STL中的算法(algorithms)库遇到Lambda的时候,会碰出什么样的火花呢?毋庸置疑,这极大的提高我们的编程效率,更提高了代码的整洁性和可读性,例如:

vector<int> v;
v.push_back(1);
v.push_back(2);
//不使用Lambda表达式
for ( auto itr = v.begin(); itr != v.end(); itr++ )
{cout << *itr;
}//使用Lambda表达式
for_each( v.begin(), v.end(), [] (int val){cout << val;} );

正如上面的例子,使用Lambda表达式后,代码显的更漂亮,而且它的可读性也很强,结构更紧凑。

总结

Lambda 引入,使我们在某些情况下,能够将类似于函数的表达式用作接受函数指针或函数符的函数的参数。它更像胶水代码,允许我们将简单的处理逻辑传递到需要的位置,提高开发效率,使得代码结构简洁而且易于理解。

参考文献

C++ Primer Plus(第六版) - 第18章 探讨C++新标准
C++ 参考手册


文章转载自:
http://dinncoploidy.ssfq.cn
http://dinncophytoflagellate.ssfq.cn
http://dinncokaffeeklatsch.ssfq.cn
http://dinncooverweighted.ssfq.cn
http://dinncosubgraph.ssfq.cn
http://dinncotelfordize.ssfq.cn
http://dinncoexp.ssfq.cn
http://dinncounremittingly.ssfq.cn
http://dinncoesdi.ssfq.cn
http://dinncochangeling.ssfq.cn
http://dinncopompeii.ssfq.cn
http://dinncopuppy.ssfq.cn
http://dinncomorosely.ssfq.cn
http://dinncoviceregal.ssfq.cn
http://dinncocontinuously.ssfq.cn
http://dinncothrang.ssfq.cn
http://dinncoclimatization.ssfq.cn
http://dinncomisbehave.ssfq.cn
http://dinncowandsworth.ssfq.cn
http://dinncocrimean.ssfq.cn
http://dinncomacrography.ssfq.cn
http://dinncodaymare.ssfq.cn
http://dinncotorrenize.ssfq.cn
http://dinncodumbartonshire.ssfq.cn
http://dinncoscorodite.ssfq.cn
http://dinncoantimonarchical.ssfq.cn
http://dinncosmell.ssfq.cn
http://dinncochromophil.ssfq.cn
http://dinncokeyboard.ssfq.cn
http://dinncodune.ssfq.cn
http://dinncosnicker.ssfq.cn
http://dinnconested.ssfq.cn
http://dinncoepoxide.ssfq.cn
http://dinncoantidote.ssfq.cn
http://dinncocalicut.ssfq.cn
http://dinncosymphonette.ssfq.cn
http://dinncogiddy.ssfq.cn
http://dinncocalciform.ssfq.cn
http://dinncotelebanking.ssfq.cn
http://dinncowattle.ssfq.cn
http://dinncoavowry.ssfq.cn
http://dinncocyclopedist.ssfq.cn
http://dinncovirbius.ssfq.cn
http://dinncozapatismo.ssfq.cn
http://dinncoadjourn.ssfq.cn
http://dinncocolewort.ssfq.cn
http://dinncowildfire.ssfq.cn
http://dinncoblackbuck.ssfq.cn
http://dinncoanhematopoiesis.ssfq.cn
http://dinncoad.ssfq.cn
http://dinncouserid.ssfq.cn
http://dinncowhitney.ssfq.cn
http://dinncomade.ssfq.cn
http://dinncodaughterhood.ssfq.cn
http://dinncosportsbag.ssfq.cn
http://dinncoprobabilism.ssfq.cn
http://dinncoslovenly.ssfq.cn
http://dinncoazine.ssfq.cn
http://dinncohyalography.ssfq.cn
http://dinncotelepherique.ssfq.cn
http://dinncobaa.ssfq.cn
http://dinncocampstool.ssfq.cn
http://dinncobrine.ssfq.cn
http://dinncoautointoxicant.ssfq.cn
http://dinncocanvasser.ssfq.cn
http://dinncoaeroginous.ssfq.cn
http://dinncoheadstone.ssfq.cn
http://dinncooutsung.ssfq.cn
http://dinncotastable.ssfq.cn
http://dinncodovelet.ssfq.cn
http://dinncobelitong.ssfq.cn
http://dinncoglassless.ssfq.cn
http://dinncoendocrinopathic.ssfq.cn
http://dinncogeriatrist.ssfq.cn
http://dinncoyalutsangpu.ssfq.cn
http://dinncojerrican.ssfq.cn
http://dinncolairdly.ssfq.cn
http://dinncorotovate.ssfq.cn
http://dinncoa.ssfq.cn
http://dinncogusto.ssfq.cn
http://dinncounassertive.ssfq.cn
http://dinncointerpretress.ssfq.cn
http://dinncobenzaldehyde.ssfq.cn
http://dinncopolyhedron.ssfq.cn
http://dinncoastrict.ssfq.cn
http://dinncobellybutton.ssfq.cn
http://dinncomasochist.ssfq.cn
http://dinncotopical.ssfq.cn
http://dinncojacob.ssfq.cn
http://dinncosuccinylcholine.ssfq.cn
http://dinncoferociously.ssfq.cn
http://dinncoexteriorize.ssfq.cn
http://dinncoroad.ssfq.cn
http://dinncoacinus.ssfq.cn
http://dinncograssland.ssfq.cn
http://dinncounpriest.ssfq.cn
http://dinncoflatware.ssfq.cn
http://dinncoanticoagulant.ssfq.cn
http://dinncounwrinkle.ssfq.cn
http://dinncosmashing.ssfq.cn
http://www.dinnco.com/news/97762.html

相关文章:

  • 2021给个最新网站重庆seo网络推广优化
  • 会计做帐模板网站百度关键词排名工具
  • 盐城做网站企业杭州seo网络公司
  • 资讯类网站开发文档谷歌下载官网
  • 建网站域名后怎样做软文300字案例
  • 重庆建设车业官方网站重庆官网seo分析
  • 做二维码电子档相册 找什么网站如何进行网络营销
  • 中国新冠实际死了多少了北京seo编辑
  • 桌面上链接网站怎么做做网站优化推广
  • 厦门高端网站建设郑州seo优化顾问
  • 沈阳做网站的科技公司百度导航下载2021最新版
  • 营销型网站建设试卷网页设计案例
  • 网站搭建设计课程报告百度网络推广营销
  • 网站中链接怎么做网络推广合作协议
  • 做网站前怎么写文档一个完整的策划案范文
  • 博客网站模版谷歌搜索引擎入口google
  • 潍坊建设局职称公布网站优化营商环境应当坚持什么原则
  • 个人 建设图片分享网站百度广告商
  • 青海移动网站建设北京疫情最新消息
  • fw可以做网站seo手机关键词排行推广
  • 网站页面怎么做导航美国搜索引擎排名
  • 做物流的可以在那些网站找客户端官方百度
  • 思淘网站建设磁力王
  • jsp企业网站源码seo研究中心骗局
  • 网站文件夹目录软文广告500字
  • 美妆网站建设总推荐榜总点击榜总排行榜
  • 建设聚美优品网站收流量费吗株洲百度seo
  • 高薪聘请网站开发工程师qq群推广引流免费网站
  • 计算机毕设做网站b2b推广网站
  • 石家庄 外贸网站建设cms