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

时间:2014-01-14 13:19:26

标签: 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;

但是它给了我错误传递const&amp;丢弃限定符。我该怎么办呢?

1 个答案:

答案 0 :(得分:6)

您需要设置运算符const,以便可以在const个实例上或通过const引用或指向const的指针调用它:

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

或者,将其设为非会员:

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