Unbound
Unbound

Reputation: 335

How to initialize priority_queue of a class in C++?

I am trying to initialize a priority_queue , Here is the code

class stdnt{
    public:
        int indx;
        int c;
        int lvl;
    bool operator<(const stdnt &x)
    {
        return this->c > x.c;
    }
};
priority_queue<stdnt> pq;

But its giving me error that passing const & discards qualifiers. How else am I supposed to do this?

Upvotes: 0

Views: 209

Answers (1)

juanchopanza
juanchopanza

Reputation: 227390

You need to make the operator const so that it can be called on const instances or via const references or pointers to const:

    bool operator<(const stdnt &x) const
                                   ^^^^^

Alternatively, make it a non-member:

bool operator<(const stdnt &lhs, const stdnt& rhs)
{
    return lhs.c > rhs.c;
}

Upvotes: 6

Related Questions