C++ 0X学习:Lambda
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(); }
2018年5月22日 14:32
可以这样编写:[](int a, int b) -> int { return a + b;}
2018年6月24日 11:28
它里面的元素不一定是经过排序的
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.
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
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
2018年9月14日 17:22
This really interesting post. Thanks for sharing!
2018年9月14日 17:22
This really interesting post. Thanks for sharing!
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.
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.
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.
2018年11月14日 12:40
I also have the same opinion as you, I am very happy about this
2018年11月14日 12:41
Perhaps I have found someone with a similar perspective. This makes me happy
2018年12月10日 16:53
I really like your website and specially this post and that's the reason I am commenting here
2018年12月14日 13:04
Its really good information, i like your blog post. Thanks for sharing
2018年12月15日 12:19
Though it's not necessary to fill those fields since it will be taken from IP pool.
2019年1月05日 16:15
Thank you for this sharing, I think it's pretty cool
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.
2019年1月14日 10:12
Thanks for all your information, Website is very nice and informative content
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.
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.
2019年3月07日 10:02
Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
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.
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
2019年4月11日 16:42
Thank you for your blog. Thanks Again. Really Great.
2019年5月10日 18:02
it's very useful post, thak you for this information
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
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.
2019年11月17日 19:52
Thank you for this sharing, I think it's pretty cool
2020年3月18日 15:26
More data is need to be collected as this data code is very useful too
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.
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
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.