目录
lambda表达式
- 也就是匿名函数,我们可以在全局区、函数内、就像声明变量一样来声明一个函数出来用,而普通函数并不能在函数内声明。
- 示例:
int main() {
// 声明
auto fun = []() {
cout << 123 << endl;
};
// 调用
fun();
return 0;
}
递归
- 对于普通函数,递归调用很简单,我们可以在函数内直接调用函数自己。
int hello(int value) {
if(value < 0) {
return 0;
}
cout << value << endl;
hello(value - 1);
}
lambda递归
- 对于 lambda 实现递归,由于它是匿名函数,本身没有函数名,我们是把他赋值给了一个变量。
- 但要注意的是,像下面这样直接调用是不行的:
- 因为在 声明lambda表达式时,变量fun应当视为还没声明,fun需要等lambda表达式构造完成后赋值过来才算完成声明,因此在 lambda 内是不能访问 fun 这个变量的。
int main() {
auto fun = [&fun](int value) {
if(value < 0) {
return;
}
cout << value << endl;
// 这里是不行的!!!
fun(value - 1);
};
fun(10);
return 0;
}
- 既然如此,我们可以在lambda表达式前先声明一个 “容器”,让它去保存lambda,并且在声明 lambda时,由于这个容器是之前声明好的,因此可以引用捕获它,然后在 lambda内取到自己调用即可:
#include <iostream>
#include <functional>
using namespace std;
int main() {
int i = 10;
std::function<void()> myfun;
myfun = [&myfun, &i]() {
if (i < 0) {
return;
}
cout << i << endl;
--i;
myfun();
};
myfun();
return 0;
}
buy cipro online without prescription: antibiotics cipro – buy cipro no rx
I quite like reading through an article that can make people think. Also, thank you for allowing me to comment.
I absolutely love your website.. Great colors & theme. Did you create this web site yourself? Please reply back as I’m planning to create my very own blog and would love to find out where you got this from or what the theme is called. Many thanks!
Having read this I thought it was extremely informative. I appreciate you taking the time and effort to put this information together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worthwhile.
Spot on with this write-up, I seriously feel this amazing site needs a great deal more attention. I’ll probably be returning to see more, thanks for the advice!
Way cool! Some extremely valid points! I appreciate you penning this write-up and the rest of the site is extremely good.
Everything is very open with a really clear description of the challenges. It was really informative. Your site is extremely helpful. Many thanks for sharing!
An interesting discussion is worth comment. I believe that you should publish more on this subject matter, it might not be a taboo matter but usually people do not talk about these issues. To the next! All the best!
I like it when individuals come together and share views. Great website, keep it up.
Hi there! This blog post couldn’t be written much better! Going through this article reminds me of my previous roommate! He always kept preaching about this. I’ll forward this post to him. Fairly certain he’ll have a great read. I appreciate you for sharing!
Hello! I simply wish to give you a huge thumbs up for your great info you’ve got right here on this post. I am coming back to your blog for more soon.
I couldn’t resist commenting. Perfectly written!
Greetings, I do think your website might be having internet browser compatibility issues. Whenever I look at your web site in Safari, it looks fine however when opening in I.E., it has some overlapping issues. I just wanted to give you a quick heads up! Aside from that, excellent website.
Hello my loved one I want to say that this post is amazing great written and include almost all significant infos I would like to look extra posts like this
I like reading through an article that will make men and women think. Also, thank you for allowing for me to comment.
When I originally commented I seem to have clicked on the -Notify me when new comments are added- checkbox and now each time a comment is added I recieve 4 emails with the same comment. Perhaps there is an easy method you can remove me from that service? Thank you.
Your style is unique compared to other people I have read stuff from. Many thanks for posting when you have the opportunity, Guess I will just book mark this web site.
The very next time I read a blog, I hope that it won’t disappoint me as much as this one. I mean, Yes, it was my choice to read, however I truly thought you’d have something helpful to say. All I hear is a bunch of moaning about something that you could possibly fix if you were not too busy seeking attention.
Your style is so unique compared to other people I have read stuff from. Thank you for posting when you have the opportunity, Guess I’ll just bookmark this site.
I could not resist commenting. Well written.