如何在c++中使用函数来创建序列
How can I use function in C++ to create a sequence?
我的目标是在c++中使用"accumulate"创建一个不可变函数(函数式编程)。我创建了一个虚拟列表,根据我发送的位置生成1,即6。所以开始的列表包含{1,1,1,1,1,1}。我尝试使用accumulate以某种方式使用该列表中的信息,并将斐波那契序列生成一个新列表。结果必须为{1,1,2,3,5,8}
这是我有的。
list<int> immutableFibonacci(int position)
{
const size_t fixedListSize(position);
list<int> newList(position, int(1));
list<int> copyList;
list<int>::iterator it = newList.begin();
if (position <=2)
{
return newList; //returns {1,1} or {1}
}
while (position>0)
{
advance(it, 1);
sum = accumulate(newList.begin(),it, 0);
copyList.push_back(sum);
position--;
}
return copyList;
}
到目前为止,我将返回copyList为{1,2,3,4,5,6}。有人能告诉我该怎么做吗?我做了很多研究。
这个方法创建了一个'类容器'对象,通过begin()
和end()
公开迭代器
#include <iterator>
#include <iostream>
struct fib_iterator : std::iterator<std::forward_iterator_tag, long long>
{
fib_iterator(std::size_t torun = 0) : to_run(torun) {}
value_type operator*() const {
return value();
}
fib_iterator& operator++()
{
--to_run;
switch(preamble)
{
case 2:
--preamble;
return *this;
case 1:
--preamble;
return *this;
}
auto next = value();
x = y;
y = next;
return *this;
}
value_type value() const
{
switch(preamble)
{
case 2:
return 0;
case 1:
return 1;
}
return x + y;
}
bool operator==(const fib_iterator& r) const {
return to_run == r.to_run;
}
bool operator!=(const fib_iterator& r) const {
return to_run != r.to_run;
}
long long x = 0;
long long y = 1;
std::size_t preamble = 2;
std::size_t to_run;
};
struct fibonacci_sequence
{
fibonacci_sequence(std::size_t length) : length_(length) {}
fib_iterator begin() const { return { length_ }; }
fib_iterator end() const { return { }; }
std::size_t length_;
};
int main()
{
for (auto i : fibonacci_sequence(50))
std::cout << i << ", ";
std::cout << 'n';
}
样本输出:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887,
9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073,
4807526976, 7778742049,
这个怎么样:
#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';
}
但是我也认为对于这么简单的事情,它的开销有点太大了。
编辑:请记住使用:g++——std=c++14 fibo.cpp -o fibo来编译它。
编辑:如果你不想使用lambda函数看这里:我如何在c++中修改这个斐波那契代码来使用函数而不是lambda?
相关文章:
- 通过构造函数创建的所有对象都具有相同的向量
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- C++ :: 如何捕获由 void 函数创建的矩阵
- 通过零参数构造函数创建的 glm::mat4 应该包含哪些值?
- 用于基于成员字段或函数创建比较器的快捷方式
- std::使用类成员函数创建线程 - 最佳实践
- 为C++重载函数创建SWIG类型映射
- 使用成员函数创建std::函数不会编译
- 如何为返回特定类型的函数创建 SWIG 类型图
- 为具有多个参数的函数创建 SWIG 类型图的更简单方法?
- 通过另一个宏创建的函数创建所有列表的宏
- std::map<int, A> 运算符 [] 需要使用空构造函数创建 A
- 无法使用 CreateWindowEx 函数创建窗口
- c++ 从成员函数创建新线程并移动对象和整个对象
- 无法使用 c++ 中的类成员函数创建/写入文件
- 使用makeword函数创建错误e0109-表观呼叫的括号前表达式必须具有(指针到 - )函数类型
- 为 OpenMP 函数创建别名 ||部分禁用 openMP
- 稍后使用<Class>调用类构造函数创建 std::vector