I'm new to lambdas, I made my own binary heap class with custom comparator function. It went well until I got a compilation error and I don't know how to fix.
I tried to change my line of code a bit, instead of
this(capacity, [](int a, int b){return a - b;});
I changed to this:
function<int(int, int)> cmp = [](int a, int b){return a - b;};
this(capacity, cmp);
I got the same result. How to deal with this error?
binary heap class:
class binaryheap
{
private:
int *heap;
int size;
int capacity;
function<int(int, int)> cmp;
int parent(int i);
int left_child(int i);
int right_child(int i);
void swap(int *a, int *b);
void heapify(int i);
public:
binaryheap(int capacity);
binaryheap(int capacity, const function<int(int, int)>& cmp);
~binaryheap();
bool empty();
int heap_size() const;
int get_root() const;
int extract_root();
void decrease_key(int i, int value);
void insert_key(int key);
void delete_key(int i);
};
The part of my code with a compilation error
binaryheap::binaryheap(int capacity)
{
this(capacity, [](int a, int b){return a - b;});//binaryheap.cpp:51:58: error: expression cannot be used as a function
}
binaryheap::binaryheap(int capacity, const function<int(int, int)>& cmp)
{
this->capacity = capacity;
this->heap = new int[capacity + 1];
this->size = 0;
this->cmp = cmp;
}
Aucun commentaire:
Enregistrer un commentaire