gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:24 25 4
gpt4 key购买 nike

我正在尝试初始化一个 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 并丢弃限定符给我带来了错误。我还应该怎么做?

最佳答案

您需要使运算符 const 以便可以在 const 实例上或通过 const 引用或指向 的指针调用它常量:

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

或者,将其设为非成员:

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

关于c++ - 如何在 C++ 中初始化类的 priority_queue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21114584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com