如何在c++中使用函数来创建序列

How can I use function in C++ to create a sequence?

本文关键字:函数 创建 c++      更新时间:2023-10-16

我的目标是在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?