Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何以泛型参数作为参数传递lambda函数?_C++_C++11_Generics_Lambda - Fatal编程技术网

C++ 如何以泛型参数作为参数传递lambda函数?

C++ 如何以泛型参数作为参数传递lambda函数?,c++,c++11,generics,lambda,C++,C++11,Generics,Lambda,我不明白如何将lambda与泛型参数一起使用,并将其作为参数传递给另一个方法。 下面是我现在拥有的代码(当然是错的): 类数组最终版 { 公众: 模板 静态空心插入排序(先随机,后随机){ 自动函数=[](随机IT lt,随机IT rt){ 返回*lt>(*rt); }; InsertSort(第一、最后、func); } 模板 静态void InsertionSortDesc(先随机,后随机){ 自动函数=[](随机IT lt,随机IT rt){ 返回*lt < P>注释:我还没有测试过你的代

我不明白如何将lambda与泛型参数一起使用,并将其作为参数传递给另一个方法。
下面是我现在拥有的代码(当然是错的):

类数组最终版
{
公众:
模板
静态空心插入排序(先随机,后随机){
自动函数=[](随机IT lt,随机IT rt){
返回*lt>(*rt);
};
InsertSort(第一、最后、func);
}
模板
静态void InsertionSortDesc(先随机,后随机){
自动函数=[](随机IT lt,随机IT rt){
返回*lt<(*rt);
};
InsertSort(第一、最后、func);
}
私人:
数组();
模板
静态void InsertSort(先随机,后随机,
std::函数func){
int length=std::距离(第一个,最后一个);
如果(长度<2){
返回;
}
RandomIt j=第一个+1;
对于(;j!=last;++j){
自动键=*j;
randomiti=j-1;
while(i>=first&&func(i,j)){
*(i+1)=(*i);
--一,;
}
*(++i)=键;
}
}
};
它在编译时崩溃并出现错误:

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);
                   ^
arrays.h:38:错误:调用“arrays::InsertSort(const int*&,const int*&,arrays::InsertionSort(RandomIt,RandomIt)[with RandomIt=const int*]::&]时没有匹配函数”
InsertSort(第一、最后、func);
^

如何正确书写?在C++ V11中可以吗?< /P> < P>注释:我还没有测试过你的代码。然而,下面的编译。函数是静态的,因此其声明必须按使用顺序进行。要修复它,请将
InsertSort
的声明移到使用它的所有其他函数之前。接下来,需要使用模板参数调用
InsertSort

例:

#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;
}
#包括
#包括
最终类数组
{
私人:
数组();
模板
静态void InsertSort(先随机,后随机,
std::函数func){
自动长度=标准::距离(第一个,最后一个);
如果(长度<2){
返回;
}
RandomIt j=第一个+1;
对于(;j!=last;++j){
自动键=*j;
randomiti=j-1;
while(i>=first&&func(i,j)){
*(i+1)=(*i);
--一,;
}
*(++i)=键;
}
}
公众:
模板
静态空心插入排序(先随机,后随机){
自动函数=[](随机IT lt,随机IT rt){
返回*lt>(*rt);
};
InsertSort(第一、最后、func);
}
模板
静态void InsertionSortDesc(先随机,后随机){
自动函数=[](随机IT lt,随机IT rt){
返回*lt<(*rt);
};
InsertSort(第一、最后、func);
}
};
int main(int argc,const char*argv[]{
向量向量向量={1,9,4,5,2,3,8,6,7};
数组::InsertionSort(vec.begin(),vec.end());
用于(自动i:vec){

老实说,我会将
InsertSort
更改为
template static void InsertSort(RandomIt first,RandomIt last,Compare&&func){…<代码> >没有理由要求该参数是<代码> STD::函数< /C>实例;任何合适的调用都应该执行。@ CDHOWIE,对不起,IMC++ NoBee在C++中。但是我在这个声明中没有看到函数的参数类型的任何检查,即,我可以传递错误的函数(与其他参数)?在这种情况下,它会在编译时崩溃吗?是的,如果在参数无法转换的情况下传递一个可调用的,模板将无法实例化。@cdhowie,我尝试了您的解决方案,但出现了一个新错误:
arrays.h:67:öббааa:只读位置的赋值'*(I+4u)'*(I+1)=(*I)^
好吧,根据您问题中的编译器错误,您正在传递一个
常量int*
(指向常量int的指针),那么您期望得到什么呢?显示调用
数组::InsertionSort()
的代码。如果使用类名限定调用,则不必先声明一个(
Arrays::InsertSort
)。或者,如果呼叫站点符合该名称,至少G++会接受该代码。谢谢大家的回答和评论!你们帮了我很大的忙!
#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;
}
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;
        }
    }
};