首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修改此代码以使用函数而不是lambda?

如何修改此代码以使用函数而不是lambda?
EN

Stack Overflow用户
提问于 2016-09-09 04:52:21
回答 3查看 117关注 0票数 2

我有一个有效的fibonacci函数,它从一个虚拟列表{1,1,1,1,1,1}中返回一个fibonacci数的列表。这是我的代码。

代码语言:javascript
运行
复制
list<int> immutableFibonacci(int position)
{
list<int> oldList(position, int(1));

list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    [](const list<int> a, int b)
{
    list<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;
});
    return newList;
}

我希望传递一个函数的名称,而不是传递拉姆达表达式const list a, int b来计算。我该怎么做呢?它基本上是函数immutableFibonacci中的一个函数,但我在这样做时遇到了麻烦。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-09 05:04:53

而不是:

代码语言:javascript
运行
复制
list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    [](const list<int> a, int b)
{
    list<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;
});

并传递一个实际的函数,如下所示。你需要函数指针:

代码语言:javascript
运行
复制
list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    funcname);

//Below is the function    

list<int> funcname(list<int> a, int b) {
    list<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;
}

对于自定义比较器,您可以传递函数指针或lambda,在您的示例中,您使用了lambda。

票数 1
EN

Stack Overflow用户

发布于 2016-09-09 12:32:33

感谢您使用我的答案:-) -> How can I use accumulate in C++ to create the Fibonacci sequence?

以下是我对您的问题的解决方案:

代码语言:javascript
运行
复制
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>



auto fibo (std::vector<int>& a, int b) 
{
     if(a.size()<2)
     {
          a.push_back(1);
     }
     else
     {
          auto start = a.rbegin();
          auto first = *start;
          start++;
          auto second = *start;
          a.push_back(first+second);
     }
     return a;
}

int main()
{
    const 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>{}, fibo);

    std::cout << "Fibo: " <<'\n';

    for( auto c : s )
    {
        std::cout << c << "-";
    }
    std::cout << '\n';
}

记得用:g++ --std=c++14 fibo.cpp -o fibo编译它。

票数 1
EN

Stack Overflow用户

发布于 2016-09-09 05:33:57

您可以在immutableFibonacci函数中创建一个函数器,如下所示:

代码语言:javascript
运行
复制
struct
{
    list<int> operator()(const list<int>& a, int b) const
    {
        //...then put your lambda code here...
    }
} funObj;

然后只需在累加函数中使用"funObj“名称,而不是lambda。就这样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39400018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档