无法在函数模板中使用 lambda 函数
Can't use the lambda function inside function template
我的函数模板有问题。
我有 3 个不同的集合,我有集合的迭代器。现在我必须创建一个函数模板"apply">,它将执行以下操作: 1. 遍历集合的所有元素并检查谓词是否为真:
1.1 如果谓词返回 true - 则需要使用 lambda"passed">更改集合元素
1.2 如果谓词返回 false = 则集合元素需要更改为 lambda"拒绝">
请给我一个例子,我应该怎么写。
非常感谢您的帮助。此处更新了代码:
#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。我该怎么做?
那么代码应该是什么样子的?你能写一个例子吗?
以下内容编译并运行,但我不确定这是否是您想要的:
#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++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 两组使用lambda函数的大括号
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 为什么我不能在 constexpr lambda 函数中使用 std::tuple
- C++:Lambda 函数指针转换的用例是什么?
- 将 lambda 函数作为参数传递C++
- 如何将 lambda 函数作为参数发送到另一个函数
- 传递 lambda 函数的权衡是什么?
- 如何使用类模拟 C++11 中的 lambda 函数和闭包?
- 不是 lambda 函数中的常量表达式
- 如何使用可变参数数重载 lambda 函数?
- 如何通过指针传递lambda函数?
- 使用带有 lambda 函数指针的模板
- openmp c++ 中并行块内 lambda 函数的奇怪行为
- C++ 中 Lambda 函数中的溢出
- 将数组传递到 lambda 函数中
- lambda 函数未显示正确的结果