- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 priority_queue 创建一个“number”的最小堆。 “number”是我定义的一个类,它有三个私有(private)变量:
int value; //This holds the value to be read in the heap
vector<int> list; //This holds the vector where the value is gotten from
size_t index; //This holds the index of the vector that is referenced
我在这里唯一关心的私有(private)变量是“值”。
我重载了 < 和 > 运算符作为 priority_queue 的先决条件(我认为同时执行这两个操作有点过分,但我在这里学习):
bool operator <(const number &x) {
return (value < x.value);
}
bool operator >(const number &x) {
return (value > x.value);
}
我的优先级队列声明在这里:
priority_queue<number, vector<number>, greater<number>> heap;
每当我尝试编译我的程序时,我都会收到此错误:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_function.h:376:20: error: no match for
'operator>'
(operand types are 'const number' and 'const number')
{ return __x > __y; }
~~~~^~~~~
In file included from main.cpp:18:0:
number.h:59:7: note: candidate: bool number::operator>(const number&) <near match>
bool operator >(const number &x) {
^~~~~~~~
我不明白为什么编译器不能使用我在“数字”类中重载的 > 运算符。有谁知道为什么会发生此错误?另外,我不确定是否需要发布更多代码来解决这个问题,因为我是使用 priority_queue 的新手。如果我这样做,请告诉我。
最佳答案
我在评论中被告知我需要在我的运算符之后添加一个常量。
bool operator <(const number &x) const {
return (value < x.value);
}
bool operator >(const number &x) const {
return (value > x.value);
}
关于c++ - 在 priority_queue 的自定义类中重载比较运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58867203/
我尝试使用 constructor 为 priority_queue 分配内存,但出现以下错误: No matching constructor for initialization of 'prio
#include #include #include #include struct Temp { int p; std::string str; }; struct Te
这个问题在这里已经有了答案: Boost heap Setting user defined compare function (1 个回答) 关闭 8 年前。 我正在尝试为自定义 Edge 类设置
我是 C++ 的初学者。我正在尝试使用 std::priority_queue 创建最大堆和最小堆。只创建一个 maxheap 可以正常工作,但不能同时创建两者。我似乎无法理解错误。我收到以下错误:无
如何清除使用用户定义比较的priority_queue? 来自 std::priority_queue documentation ,我将 priority_queue 的使用减少到我需要的情况(=
如何清除使用用户定义比较的priority_queue? 来自 std::priority_queue documentation ,我将 priority_queue 的使用减少到我需要的情况(=
我有一个类Node除了存储数据外,它还有一个指向其父节点的指针 Node .我将一些节点存储在 priority_queue 中并覆盖 queue; 问题是,父指针似乎搞砸了。我的猜测是,当我弹出
我想创建一个名为 Edge 的对象,它从其构造函数将自身插入到 priority_queue 中。也就是; Class Edge { int m_from; int m_to; in
代码如下: 比较算法 class PathComp{ public: virtual bool betterThan(const PathInfo& path1, const PathInfo& pa
我有 priority_queue。我的函数 delete 在内存消耗和时间方面不是很合理。我见过类似的主题,但它们对我没有帮助。 How to remove element not at top f
我有这样的代码 priority_queue, decltype(&VD::CompareByDistance)> pqDistances(&VD::CompareByDistance); 在哪里 c
我正在研究最小堆的解决方案,除了自定义比较器之外,它还需要支持删除任何元素。完全自定义的堆实现是一种方法。但我想依靠 C++ STL 来进行所需的操作。 C++ 文档和 StackOverflow 答
我有一个 Dijkstra使用 priority_queue 的类具有自定义比较功能。我将队列命名为 DijkstraPriorityQueue用using陈述。在类构造函数中,我初始化了队列。为此,
我正在为这个问题编写代码。当我遇到问题时,整数流的中位数。请注意,此问题不是算法问题,而是 priority_queue 大小的模糊行为。 #include using namespace std;
int main() { list letters; priority_queue, less>letters_trans; cout input(cin), input_e
我的文件顶部有这些: #include typedef struct cell_s { unsigned int x; unsigned int y; unsigned in
你好,我需要创建一个类,其中包含一个 priority_queue 字段,其比较函数需要访问类中的另一个字段。简而言之,我需要写这样的东西: class A { B foo; prio
当我尝试使用 priority_queue 作为类成员时,我卡住了。请查看下面的代码,让我知道为什么 L1 看不到 Type 但 L2 可以看到。我尝试了 struct,也尝试了 ctor。 如果这是
我有一个服务器应用程序,它接受传入的查询并执行它们。如果查询太多,则应将它们排队,如果执行了其他一些查询,则也应执行排队的查询。由于我想传递具有不同优先级的查询,我认为使用 priority_queu
考虑一个 std::priority_queue,其中 N 元素具有相同的优先级。现在考虑具有任意优先级的元素的一些 pop() 和 push(),因此生成的队列由所有这些 N 元素组成上面提到的加上
我是一名优秀的程序员,十分优秀!