main.cpp
· 398 B · C++
Originalformat
```cpp
#include <iostream>
#include <functional>
std::function<int()> fibonacci()W
{
int current = 1, next = 1;
return [=]() mutable {
int result = current;
next = current + (current = next);
return result;
};
}
int main()
{
auto fib = fibonacci();
for (int i = 0; i < 10; i++) {
std::cout << fib() << std::endl;
}
return 0;
}
```
| 1 | |
| 2 | ```cpp |
| 3 | #include <iostream> |
| 4 | #include <functional> |
| 5 | |
| 6 | std::function<int()> fibonacci()W |
| 7 | { |
| 8 | int current = 1, next = 1; |
| 9 | |
| 10 | return [=]() mutable { |
| 11 | int result = current; |
| 12 | next = current + (current = next); |
| 13 | return result; |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | int main() |
| 18 | { |
| 19 | auto fib = fibonacci(); |
| 20 | |
| 21 | for (int i = 0; i < 10; i++) { |
| 22 | std::cout << fib() << std::endl; |
| 23 | } |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | ``` |