更新时间:2023-02-18 20:03:11
如何:
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
int main()
{
std::vector<int> v{1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
std::vector<int> s = std::accumulate(v.begin(), v.end(),std::vector<int>{},
[](const std::vector<int>& a, int b)
{
std::vector<int> d = a;
if(a.size()<2)
{
d.push_back(1);
}
else
{
auto start = d.rbegin();
auto first = *start;
start++;
auto second = *start;
d.push_back(first+second);
}
return d;
});
std::cout << "Fibo: " <<'\n';
for( auto c : s )
{
std::cout << c << "-";
}
std::cout << '\n';
}
但我也认为这是一个有点开销, 。
But I also think it is a bit too much overhead, for something that simple.
编辑:记住编译:g ++ --std = c ++ 14 fibo.cpp -o fibo。
Remember to compile that with: g++ --std=c++14 fibo.cpp -o fibo.
If you don't want to use the lambda function look here: How can I modify this Fibonacci code in C++ to use a function instead of lambda?