简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM