目录
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 prednisone online no script: raypharm – prednisone tabs 20 mg
get cheap clomid without insurance: cheap clomid – order cheap clomid without prescription
You should be a part of a contest for one of the best sites on the net. I’m going to highly recommend this blog!
dapoxetine online: buy dapoxetine online – priligy max pharm
buy generic clomid without dr prescription: clomid purchase online rex pharm – get cheap clomid no prescription
buy prednisone canadian pharmacy: prednisoneraypharm – prednisone uk price
dapoxetine price: Priligy tablets – max pharm
The next time I read a blog, Hopefully it does not fail me just as much as this one. After all, I know it was my choice to read, however I truly thought you would have something interesting to talk about. All I hear is a bunch of whining about something that you can fix if you were not too busy seeking attention.
I blog often and I genuinely thank you for your content. Your article has truly peaked my interest. I am going to book mark your website and keep checking for new information about once a week. I subscribed to your Feed too.
buy dapoxetine online: priligy max pharm – buy priligy max pharm
This article is fantastic! Full of valuable information and extremely well-written. Thank you for offering this.
cost generic clomid: cheap clomid – cost clomid tablets
5 mg prednisone tablets: raypharm – buy prednisone without a prescription best price
can i purchase generic clomid online: generic clomid – order cheap clomid without rx
priligy max pharm: priligy – dapoxetine price
Узнай все о на что влияет варикоцеле варикоцеле
Узнай все о степени варикоцеле варикоцеле
Узнай все о варикоцеле варикоцеле 2 степени
Cat Cazino Скачать на Андроид. https://www.pgyer.com/apk/apk/com.cat.c115546
priligy max pharm: cheap priligy – priligy
Excellent post. It’s extremely clear and filled with valuable details. Many thanks for sharing this content.
where to buy generic clomid without prescription get cheap clomid pills get clomid online
Can I simply say what a relief to discover somebody that truly understands what they’re discussing on the net. You definitely realize how to bring a problem to light and make it important. More and more people ought to look at this and understand this side of the story. I can’t believe you’re not more popular given that you certainly have the gift.
cost of generic clomid without insurance: cheap clomid – cheap clomid now
Adored the information shared in this post. It’s very well-written and packed with valuable insight. Great work!