compile error when using custom comparision function for priority queue (C++)


  • 0
    I

    I am giving my custom comparison function(to stl priority queue, C++) following the approach discussed here. But even after trying to debug for a long time, I cannot understand the cause of the error which I am getting -

    required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ListNode**, std::vector<ListNode*> >; _Distance = long int; _Tp = ListNode*; _Compare = __gnu_cxx::__ops::_Iter_comp_val<std::function<bool(const ListNode*&, const ListNode*&)> >]'
    

    I would really appreciate if some one could point out my mistake.
    My code is pasted below -

    bool Compare( const ListNode* &l, const ListNode* &r)
    {
        return l->val < r->val;
    }
    
    class Solution {
    public:
    
        
        ListNode* mergeKLists(vector<ListNode*>& lists) {
    
    
            priority_queue <ListNode*, vector <ListNode *>, function<bool( const ListNode* &l, const ListNode* &r)> > q(Compare);
            ListNode* x,*x_return;
    
            for(int i =0;i<lists.size();i++)
                    q.push(lists[i]);
    
            
            x_return = q.top();
    
            while(!q.empty())
            {
            x = q.top();
    
                if(q.top()->next!=NULL)
                {
                    q.pop();
                    q.push(x->next);
                }
                    
            x = x->next;
            }
            
            return x_return;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.