kendall
kendall

Reputation: 23

Error using set of custom class

When I try to compile the following code, I get

'const elem' is not derived from 'const std::__cxx11::sub_match<_BiIter>'

I don't understand what it means and what is wrong with my code?

struct elem
{
    int x,y,val;
    elem(int x,int y,int val)
    {
        this->x = x;
        this->y = y;
        this->val = val;
    }
    bool operator<(const elem b)
    {
        return val > b.val;
    }
};


int kthSmallest(int v[MAX][MAX], int n, int k)
{
    set<pair<int,int>> m;
    priority_queue<elem, vector<elem>> pq;
    int temp = k;
    pq.emplace(0,0,v[0][0]);
    while(temp != 0)
    {
        temp--;
        if(pq.top().x + 1 < n && m.find(make_pair(pq.top().x + 1, pq.top().y)) == m.end())
        {
            m.insert(make_pair(pq.top().x + 1, pq.top().y));
            pq.emplace(pq.top().x + 1, pq.top().y, v[pq.top().x + 1][pq.top().y]);
        }
        if(pq.top().y + 1 < n && m.find(make_pair(pq.top().x + 1, pq.top().y)) == m.end())
        {
            m.insert(make_pair(pq.top().x, pq.top().y + 1));
            pq.emplace(pq.top().x, pq.top().y + 1, v[pq.top().x][pq.top().y + 1]);
        }
    }
    return pq.top().val;
}

Upvotes: 0

Views: 175

Answers (1)

psur
psur

Reputation: 4519

Third template argument of std::priority_queue is Compare - binary operator which is by default std::less. It needs both arguments as const. Your first argument of operator< (which is by default object on which you call it) is not const because the method is not const. That is why you need to change it as pointed out in comments:

bool operator<(const elem b) const
{
    return val > b.val;
}

Upvotes: 1

Related Questions