如何将lambda功能传递给通用参数作为参数

How to pass a lambda-function with generic arguments as parameter?

本文关键字:参数 lambda 功能      更新时间:2023-10-16

我不明白如何将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;
        }
    }
};