C++實現閉包|斐波那契數列|mutable

實現斐波那契數列

#include<bits/stdc++.h>
using namespace std;

auto adder() {
	auto a = 0, b = 1;
	return [=]() mutable {
		b = a + b;
		a = b - a;
		return a;
	};
}

int main() {
	auto f = adder();
	cout << f() << endl;
	cout << f() << endl;
	cout << f() << endl;
	cout << f() << endl;
	cout << f() << endl;
	cout << f() << endl;
	cout << f() << endl;
	system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章