C++ 模板类中的比较器

C++ 模板类中的比较器,c++,C++,我正在研究堆实现。它必须是模板类,我需要它有自己的比较器,传入构造函数。我怎么做 我试过这个: template <typename T> class Heap{ public: Heap(int size, bool (*comparator) (const T & a, const T & b) = [] (const T & a, const T & b){ return a < b; }) //

我正在研究堆实现。它必须是模板类,我需要它有自己的比较器,传入构造函数。我怎么做

我试过这个:

template <typename T>
class Heap{
public:
    Heap(int size, bool (*comparator) (const T & a, const T & b) = [] (const T & a, const T & b){
        return a < b;
    })

    // other unimportant methods
}
模板
类堆{
公众:
堆(整数大小,布尔(*比较器)(常数T&a,常数T&b)=[](常数T&a,常数T&b){
返回a
它的作用是:

Heap<int> heap(4);
堆(4);
以及:

Heap<int> heap(4, [](const int & a, const int & b){ return false; })
堆堆(4,[](const int&a,const int&b){return false;})
但是当我尝试将它与这样的指针一起使用时(其中offer是某种结构):

heap2(3,[](提供常数*a,提供常数*b){
返回false;
});
我得到了这个编译错误:

test.cpp: In function ‘int main()’:
test.cpp:126:3: error: invalid user-defined conversion from ‘main()::<lambda(const Offer*, const Offer*)>’ to ‘bool (*)(Offer* const&, Offer* const&)’ [-fpermissive]
  });
   ^
test.cpp:124:59: note: candidate is: main()::<lambda(const Offer*, const Offer*)>::operator bool (*)(const Offer*, const Offer*)() const <near match>
  Heap<Offer*> heap2(3, [](Offer const * a, Offer const * b){
                                                           ^
test.cpp:124:59: note:   no known conversion from ‘bool (*)(const Offer*, const Offer*)’ to ‘bool (*)(Offer* const&, Offer* const&)’
test.cpp:13:5: note:   initializing argument 2 of ‘Heap<T>::Heap(int, bool (*)(const T&, const T&)) [with T = Offer*]’
     Heap(int size, bool (*comparator) (const T & a, const T & b) = [] (const T & a, const T & b){
test.cpp:在函数“int main()”中:
test.cpp:126:3:错误:用户定义的从“main()::”到“bool(*)(Offer*const&,Offer*const&)”的转换无效[-fpermissive]
});
^
test.cpp:124:59:注意:候选项是:main()::运算符bool(*)(const Offer*,const Offer*)()const
堆heap2(3,[](提供常数*a,提供常数*b){
^
test.cpp:124:59:注意:没有已知的从“bool(*)(const Offer*,const Offer*)”到“bool(*)(Offer*const&,Offer*const&)”的转换
test.cpp:13:5:注意:正在初始化“Heap::Heap(int,bool(*)(常数T&,常数T&)[with T=Offer*]”的参数2
堆(整数大小,布尔(*比较器)(常数T&a,常数T&b)=[](常数T&a,常数T&b){

如何使其在这两种情况下都能工作?

对于指针版本:“T”是一个指针,需要作为常量引用进行传递-此处常量限定的不是它指向的数据,而是指针引用本身

template <typename T>
class Heap{
public:
    Heap(int size, bool (*comparator) (const T & a, const T & b)
                                 = [] (const T & a, const T & b){
        return a < b;
    }) {}; 

    // other unimportant methods
};


int main()
{
    Heap<int*> heap2(3, [](int* const& a, int* const& b){
        return false;
    });

    // analogous to  
    Heap<int> heap(4, [](const int & a, const int & b){ 
         return false; 
    });
    // but it's the pointer to the datum in the first,
    // and the datum itself in the latter case
}
模板
类堆{
公众:
堆(整数大小,布尔(*比较器)(常数T&a,常数T&b)
=[](常数T&a、常数T&b){
返回a

在lambda的参数中尝试
提供*const&
,但该参数不起作用。函子在某些方面更好吗?
template <typename T>
class Heap{
public:
    Heap(int size, bool (*comparator) (const T & a, const T & b)
                                 = [] (const T & a, const T & b){
        return a < b;
    }) {}; 

    // other unimportant methods
};


int main()
{
    Heap<int*> heap2(3, [](int* const& a, int* const& b){
        return false;
    });

    // analogous to  
    Heap<int> heap(4, [](const int & a, const int & b){ 
         return false; 
    });
    // but it's the pointer to the datum in the first,
    // and the datum itself in the latter case
}