打酱油的萧萧

一个打酱油的程序员
C++ 0X学习:auto关键词
C++ 0X学习:for的新语法使用心得

C++ 0X学习:Lambda

萧萧 posted @ 2011年9月15日 22:49 in C/C++ with tags c++ 0X lambda , 5285 阅读

C++0x 终于支持 Lambda 了,以后使用STL算法的时候,不必那么麻烦编写函数对象了。

C++0x 的语法还是比较简单。下面通过几个例子来介绍下。

先是通过 std::for_each 演示一个简单的例子:

 

#include <iostream>
#include <vector>
#include <algorithm>

int main() {

	std::vector<int> my_vector({1, 2, 3, 4 ,5});

	std::for_each(my_vector.begin(), my_vector.end(),
			[](int value) { std::cout << value << std::endl; });


	return 0;
}

首先,是不是发现 my_vector 的构造函数怪怪的呢。因为C++0x支持一种从C带来的初始化列表概念,

语法类似于 C 的 int array[] = {1, 2, 3, 4 ,5}。关于这个详细介绍,有机会下次再介绍

1.Lambda 的格式

对于这里的 lambda:[](int value) { std::cout << value << std::endl; }

从这里看出,lambda 是以 [] 开头的匿名函数。这个lambda 仅仅把参数打印出来,是一个 void 的函数。

对于需要返回值的 lambad,格式如下:[](int a, int b) { return a + b;}

在这里,返回值的类型是 decltype(a + b),decltype是一个新关键词,用于在编译期推导出类型。

如果想强制设定返回值类型,可以这样编写:[](int a, int b) -> int { return a + b;}

这里把返回值设置为 int 型。

2. [] 的表示方式

C++ 的 lambad 是支持闭包的。那么,是通过 [] 来表示 lambda 的变量集合,即闭包。

 

[] //没有定义变量,使用未定义的变量会导致错误。
[a, &b] //a以传值方式传入,b以引用方式传入
[&] // 表示在 lambda所使用到的变量,皆以引用方式传入
[=] // 表示在 lambda所使用到的变量,皆以传值方式传入
[&, a] //表示 a 以传值方式传入,其他都是以引用方式传入
[=, &a] // 表示 a 以引用方式传入,其他都是以传值方式传入

如果企图修改以值传入的变量,则编译的时候会报错(gcc 4.5)

下面来看看一个闭包的lambda例子:

 

#include <iostream>
#include <vector>
#include <algorithm>

int main() {

	std::vector<int> my_vector({1, 2, 3, 4 ,5});

	int total = 0;

	std::for_each(my_vector.begin(), my_vector.end(),
		[&total](int value) {
			total++;
		});

	std::cout << "total:" << total << std::endl;

	return 0;
}

这里的输出结果是: total:5

3.涉及 this 指针的使用

在成员函数中涉及到对象的 this 指针,必须显式地传入lambda函数,否则成员函数的lambda函数无法使用任何对象的变量或者函数,格式如下:

[this]() { this->call_function(); }

happy room 说:
2018年5月22日 14:32

可以这样编写:[](int a, int b) -> int { return a + b;}

color switch 说:
2018年6月24日 11:28

它里面的元素不一定是经过排序的

spanish dictionary 说:
2018年7月05日 15:29

I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

cat mario 说:
2018年8月03日 09:56

I know there will be many difficulties and challenges but I am determined to do it. If it does not succeed then it will be a lesson for me as well

Superfighters 2 说:
2018年8月24日 15:52

I like to walk in the afternoon. That helps me to exercise, breathe the air and get along with nature. Watching people walk, is fun

hotmail login 说:
2018年9月14日 17:22

This really interesting post. Thanks for sharing!

hotmail login 说:
2018年9月14日 17:22

This really interesting post. Thanks for sharing!

color swtich 说:
2018年10月12日 11:02

Thank for your writting! It is easy to understand and detailed. I feel it is interesting, I hope you continue to have such good posts.

run 3 说:
2018年10月12日 11:03

Thank for your writting! It is easy to understand and detailed. I feel it is interesting, I hope you continue to have such good posts.

www.talktowendys.com 说:
2018年10月22日 21:54

The Wendy conducts the survey to know about customers satisfaction and experience. For that, they ask the customers in the form of a survey.

instagram tools 说:
2018年11月14日 12:40

I also have the same opinion as you, I am very happy about this

youtube live 说:
2018年11月14日 12:41

Perhaps I have found someone with a similar perspective. This makes me happy

Swords And Souls 说:
2018年12月10日 16:53

I really like your website and specially this post and that's the reason I am commenting here

go movies 说:
2018年12月14日 13:04

Its really good information, i like your blog post. Thanks for sharing

happy wheels 说:
2018年12月15日 12:19

Though it's not necessary to fill those fields since it will be taken from IP pool.

girlsgogames 说:
2019年1月05日 16:15

Thank you for this sharing, I think it's pretty cool

punter 说:
2019年1月08日 23:42

I used a simple feature site to create a dual monitor in window 10. you can also create here https://windowsclassroom.com/setup-dual-monitors-windows-10/ a dual monitor system in window 10.

run 4 说:
2019年1月14日 10:12

Thanks for all your information, Website is very nice and informative content

happy wheels 说:
2019年1月26日 11:37

I often get emotionally involved. Like when I read your post, I can cry, laugh, sad, funny, depending on your written words. I think you are a sensitive person.

iq ball 说:
2019年1月31日 16:50

Please treat each other well. Everybody has difficulties that we have to overcome, do not know later but how to live each day happily.

Vex 3 说:
2019年3月07日 10:02

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

Fnaf Sister Location 说:
2019年3月09日 15:44

If there are many sad stories, share it with the people you trust. Sharing helps people get closer together and you also relieve some of that sadness.

free sports streamin 说:
2019年3月26日 20:19

Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

vex 4 说:
2019年4月11日 16:42

Thank you for your blog. Thanks Again. Really Great.

hotmail.com 说:
2019年5月10日 18:02

it's very useful post, thak you for this information

David 说:
2019年7月18日 01:18

I am really happy to say it’s an interesting post to read . I learn new information from your article, you are doing a great job. Keep it up

DJMaza 说:
2019年11月17日 16:19

Please treat each other well. Everybody has difficulties that we have to overcome, do not know later but how to live each day happily.

Movies Counter 说:
2019年11月17日 19:52

Thank you for this sharing, I think it's pretty cool

happy wheels 说:
2020年3月18日 15:26

More data is need to be collected as this data code is very useful too

unblock game website 说:
2020年5月23日 15:34

Looking for the best sites for unblocked games? This article helps you choose the best unblocked games website the easy way.

celebrity birthdays 说:
2020年8月14日 17:18

It's a good chance to know more about these stories, very interesting and meaningful. And please take a look at this site, where I find the all the information I need about the celebrity birthdays

kuttyrockers 说:
2020年11月26日 19:37

I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter