如何将lambda功能传递给通用参数作为参数
How to pass a lambda-function with generic arguments as parameter?
我不明白如何将lambda与通用参数一起使用,并在另一种方法中将其作为参数传递。
在我现在拥有的代码下方(当然是错误的(:
class Arrays final
{
public:
template<class RandomIt>
static void InsertionSort(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt > (*rt);
};
InsertSort(first, last, func);
}
template<class RandomIt>
static void InsertionSortDesc(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt < (*rt);
};
InsertSort(first, last, func);
}
private:
Arrays();
template<class RandomIt>
static void InsertSort(RandomIt first, RandomIt last,
std::function<bool (RandomIt, RandomIt)> func) {
int length = std::distance(first, last);
if (length < 2) {
return;
}
RandomIt j = first + 1;
for (; j != last; ++j) {
auto key = *j;
RandomIt i = j - 1;
while (i >= first && func(i, j)) {
*(i + 1) = (*i);
--i;
}
*(++i) = key;
}
}
};
它在编译时间中崩溃了错误:
arrays.h:38: error: no matching function for call to 'Arrays::InsertSort(const int*&, const int*&, Arrays::InsertionSort(RandomIt, RandomIt) [with RandomIt = const int*]::<lambda(const int*, const int*)>&)'
InsertSort(first, last, func);
^
如何正确编写?在C V11中是否有可能?
注意:我尚未测试您的代码。但是,以下编译。该功能是静态的,因此其声明必须按照顺序进行。要修复它,请将InsertSort
的声明移至使用所有其他功能之前。接下来,您需要使用模板参数调用InsertSort
。
ex:
#include <iostream>
#include <vector>
class Arrays final
{
private:
Arrays();
template<class RandomIt>
static void InsertSort(RandomIt first, RandomIt last,
std::function<bool (RandomIt, RandomIt)> func) {
auto length = std::distance(first, last);
if (length < 2) {
return;
}
RandomIt j = first + 1;
for (; j != last; ++j) {
auto key = *j;
RandomIt i = j - 1;
while (i >= first && func(i, j)) {
*(i + 1) = (*i);
--i;
}
*(++i) = key;
}
}
public:
template<class RandomIt>
static void InsertionSort(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt > (*rt);
};
InsertSort<RandomIt>(first, last, func);
}
template<class RandomIt>
static void InsertionSortDesc(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt < (*rt);
};
InsertSort<RandomIt>(first, last, func);
}
};
int main(int argc, const char * argv[]) {
std::vector<int> vec = {1, 9, 4, 5, 2, 3, 8, 6, 7};
Arrays::InsertionSort(vec.begin(), vec.end());
for (auto i : vec) {
std::cout<<i<<" ";
}
std::cout<<std::endl;
return 0;
}
如评论中指出的:D如果您将呼叫符合到功能的呼叫,则不必更改声明订单。
ex: Arrays::InsertSort<RandomIt>(....);
class Arrays final
{
public:
template<class RandomIt>
static void InsertionSort(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt > (*rt);
};
Arrays::InsertSort<RandomIt>(first, last, func);
}
template<class RandomIt>
static void InsertionSortDesc(RandomIt first, RandomIt last) {
auto func = [](RandomIt lt, RandomIt rt) {
return *lt < (*rt);
};
Arrays::InsertSort<RandomIt>(first, last, func);
}
private:
Arrays();
template<class RandomIt>
static void InsertSort(RandomIt first, RandomIt last,
std::function<bool (RandomIt, RandomIt)> func) {
auto length = std::distance(first, last);
if (length < 2) {
return;
}
RandomIt j = first + 1;
for (; j != last; ++j) {
auto key = *j;
RandomIt i = j - 1;
while (i >= first && func(i, j)) {
*(i + 1) = (*i);
--i;
}
*(++i) = key;
}
}
};
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何将lambda作为模板类的成员函数参数
- 使用自动推导的 lambda 参数作为常量表达式
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 引用捕获和在 lambda 中通过引用发送参数有什么区别 (C++)
- 将__device__ lambda 作为参数传递给 __global__ 函数
- 将 lambda 函数作为参数传递C++
- 如何将 lambda 函数作为参数发送到另一个函数
- Lambda可以用作非类型模板参数吗
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 如何确定捕获不可复制参数的 lambda 的类型?
- 省略C++可变参数 lambda 中的"auto"关键字?
- 如何访问可变参数 lambda 函数参数
- 可变参数 lambda 捕获的解决方法
- 传递给多个参数 lambda 表达式的参数
- 如何编写丢弃其参数的通用可变参数 lambda
- 使用c++参数lambda函数
- 将函数模板"pass"为泛型可变参数 lambda 返回语句是好方法吗?
- c++模板非类型参数lambda函数