目录
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;
}
Having read this I thought it was rather enlightening. I appreciate you taking the time and energy to put this information together. I once again find myself spending a significant amount of time both reading and leaving comments. But so what, it was still worthwhile!
You’re so cool! I don’t think I have read a single thing like that before. So great to discover another person with some original thoughts on this subject matter. Really.. thank you for starting this up. This web site is something that is needed on the internet, someone with some originality.
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
Having read this I thought it was really informative. I appreciate you finding the time and energy to put this article together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worth it!
the best ed pills: Canada pharmacy – best cure for ed
Phim sex cưỡng hiếp trẻ em
https://canadiandrugsgate.com/# drug prices
It’s hard to find experienced people about this topic, but you seem like you know what you’re talking about! Thanks
https://canadiandrugsgate.com/# ed prescription drugs
Good write-up. I certainly appreciate this website. Keep writing!
Everything is very open with a precise description of the challenges. It was truly informative. Your site is very useful. Many thanks for sharing.
https://canadiandrugsgate.com/# ed medications comparison
http://indianpharmacyeasy.com/# indian pharmacy online
Тут можно преобрести сейфы для оружия пистолетные оружейный сейф на заказ москва
Тут можно преобрести сейф для оружия купить сейф для оружия цены
Сервисный центр предлагает замена акб prestigio muze a3 заменить заднюю крышку prestigio muze a3
http://canadiandrugsgate.com/# canadian drug
Сервисный центр предлагает ремонт wexler tab 700 цены ремонт wexler tab 700 цены
Pretty! This has been an incredibly wonderful post. Many thanks for providing this information.
buying prescription drugs in mexico online Mexican Pharm Gate purple pharmacy mexico price list
Excellent write-up. I definitely appreciate this website. Thanks!
Тут можно преобрести купить сейф оружейный в москве оружейный сейф для пистолета
Тут можно преобрести шкаф оружейный купить купить сейф с доставкой в москве
buying prescription drugs in mexico online: mexicanpharmgate.com – mexican mail order pharmacies
http://mexicanpharmgate.com/# mexico pharmacies prescription drugs
best online pharmacies in mexico: mexico pharmacy – medicine in mexico pharmacies
I’m impressed, I must say. Seldom do I encounter a blog that’s equally educative and entertaining, and let me tell you, you’ve hit the nail on the head. The issue is something too few people are speaking intelligently about. Now i’m very happy I came across this during my hunt for something relating to this.
Hello there, I think your blog may be having browser compatibility problems. When I take a look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I just wanted to give you a quick heads up! Other than that, wonderful site.
amoxicillin without a doctor’s prescription: canadiandrugsgate – buy prescription drugs from india
Тут можно преобрести огнестойкий сейф сейф огнестойкий купить
https://indianpharmacyeasy.com/# reputable indian pharmacies
Wonderful post! We are linking to this great article on our website. Keep up the great writing.
mexican drugstore online MexicanPharmGate mexico drug stores pharmacies
medication from mexico pharmacy: mexicanpharmgate.com – mexican rx online
There are dome tents, frame tents, wall tents, and cabin tents.
Тут можно преобрести сейф огнестойкий сейф жаростойкий
http://mexicanpharmgate.com/# buying from online mexican pharmacy
Pretty! This was an extremely wonderful post. Many thanks for providing this info.
I was more than happy to uncover this site. I wanted to thank you for your time for this wonderful read!! I definitely really liked every bit of it and i also have you book marked to check out new things on your site.
http://indianpharmacyeasy.com/# top online pharmacy india
https://canadiandrugsgate.com/# best non prescription ed pills
https://mexicanpharmgate.com/# mexican online pharmacies prescription drugs
Online medicine home delivery: Online medicine home delivery – indian pharmacy paypal
http://canadiandrugsgate.com/# what is the best ed drug
Тут можно преобрести стоимость сейфа для оружия оружейные сейфы и шкафы для охотничьего ружья
https://mexicanpharmgate.com/# buying from online mexican pharmacy
best erection pills: canadian pharmacy drugs gate – best natural cure for ed
Тут можно преобрести купить сейф для оружия купить сейф для оружия
Hi, There’s no doubt that your site might be having web browser compatibility problems. When I take a look at your site in Safari, it looks fine however when opening in IE, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, excellent site.
online ed medications: Canada pharmacy – natural ed treatment