c++ - 无法在函数模板中使用 lambda 函数

标签 c++ c++11 templates lambda std-function

我的函数模板有问题。

我有 3 个不同的集合,我有集合的迭代器。现在我必须创建一个函数模板 'apply' 它将执行以下操作: 1.遍历集合的所有元素并检查predicate是否为真:

1.1 如果谓词返回真 - 那么集合的元素需要用 lambda 'passed' 改变

1.2 if predicate return false = then element of collection need to be changed with lambda 'rejected'

请举例说明我应该怎么写。

非常感谢您的帮助。此处更新代码:

#include <iostream>
#include <vector>
#include <list>
#include <functional>

using namespace std;

template<typename T>
void apply(T collBegin, T collEnd, function<bool(int)> f1, function<int(int)> f2, function<int(int)> f3)
{
    for (T actualPosition = collBegin; actualPosition != collEnd; ++actualPosition) {
        if (f1(*actualPosition)) {
            //the argument matches the predicate function f1
            *actualPosition = f2(*actualPosition);
        }
        else {
            //the argument doesn't match the predicate function f1
            *actualPosition = f3(*actualPosition);
        }
    }
}

int main()
{
    int arr[]{ 1,2,3,4,5,6,7,8,9 };

    auto predicate = [](int arg) -> bool { return arg % 2 == 0; };

    auto passed = [](int arg) -> int { return arg / 2; };

    auto rejected = [](int arg) -> int { return (3 * arg) + 1; };

    apply(arr, arr + std::size(arr), predicate, passed, rejected);

    std::vector<int> vec(arr, arr + std::size(arr));
    apply(vec.begin(), vec.end(), predicate, passed, rejected);

    std::list<int> lis(vec.begin(), vec.end());
    apply(lis.begin(), lis.end(), predicate, passed, rejected);


    for (auto e : lis) std::cout << e << " ";
    std::cout << '\n';
}

此代码有效。但我想将其从 int 更改为 T。我该怎么做?

最佳答案

Sooo how the code should be looks like ? Can you write an example ?

下面编译并运行,但我不确定这是否是您想要的:

#include <iostream>
#include <vector>
#include <list>
#include <functional>
#include <algorithm>

template<typename T, typename U>
void apply(T collBegin, T collEnd, std::function<bool(U const &)> f1, std::function<U(U const &)> f2, std::function<U(U const &)> f3)
{
    std::for_each(collBegin, collEnd, [&](auto &el) { el = f1(el) ? f2(el) : f3(el); });
}

int main()
{
    std::function<bool(int const &)> predicate = [](int const &arg) -> bool { return arg % 2 == 0; };
    std::function<int(int const &)> passed = [](int const &arg) -> int { return arg / 2; };
    std::function<int(int const &)> rejected = [](int const &arg) -> int { return (3 * arg) + 1; };

    int arr[]{ 1,2,3,4,5,6,7,8,9 };
    apply(arr, arr + sizeof(arr)/sizeof(int), predicate, passed, rejected);

    std::vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
    apply(vec.begin(), vec.end(), predicate, passed, rejected);

    std::list<int> lis(vec.begin(), vec.end());
    apply(lis.begin(), lis.end(), predicate, passed, rejected);

    for (auto e : lis) std::cout << e << " ";
    std::cout << '\n';
}

https://ideone.com/A30Dl9

1 2 16 4 4 5 34 1 7 

关于c++ - 无法在函数模板中使用 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554161/

相关文章:

C++ 在 ‘value_type’ 中没有名为 ‘struct std::iterator_traits<int>' 的类型

c++ - C++ 库中的模板类定义

c++ - 安装 gcc 4.7 但无法成功运行程序

c++ - 我可以使用 cmpxchg16b 以原子方式将指针复制到指针和 int,同时递增 int(原子引用计数)吗?

c++ - 如何防止 MFC 对话框在 Enter 和 Escape 键上关闭?

c++ - 为什么匹配模板类上的部分类模板特化与没有模板匹配的另一个部分特化不明确?

c++ - 模板中的奇怪类型名称和构造函数

c++ - 那里有 C++0x 的实现吗?

c++ - 如何使用 Google Test 测试 EXE? (2)

c++ - Json::Value 返回时会发生变化吗?