繁体   English   中英

如何使用C ++中的函数创建序列?

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

我的目标是使用C ++中的“累加”来创建一个不变的函数(函数式编程)。 我创建了一个虚拟列表,该列表根据我发送的位置生成1,该位置将为6。因此,开头的列表包含{1,1,1,1,1,1,1}。 我尝试使用累加以某种方式使用此列表中的信息,并使斐波那契序列成为新列表。 结果必须为{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 ++中修改此Fibonacci代码以使用函数而不是lambda?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM