如何在C++中初始化类的priority_queue?

Unb*_*und 0 c++ priority-queue

我正在尝试初始化priority_queue,这是代码

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

但它给了我传递const&discards限定符的错误.我还应该怎么做呢?

jua*_*nza 6

您需要创建运算符,const以便可以在const实例上或通过const引用或指针调用它const:

    bool operator<(const stdnt &x) const
                                   ^^^^^
Run Code Online (Sandbox Code Playgroud)

或者,将其设为非会员:

bool operator<(const stdnt &lhs, const stdnt& rhs)
{
    return lhs.c > rhs.c;
}
Run Code Online (Sandbox Code Playgroud)