小编Jak*_*ers的帖子

具有自定义类型和比较器的 C++ 优先级队列不起作用

我正在尝试将 C++ STL 优先级队列与自定义类型和比较器一起使用,但无论我怎么说,我都会收到错误消息。

有谁知道可能是什么问题?我正在尝试从文档中复制语法,但没有任何效果...

自定义类型是指向 LeetCode 中使用的 ListNode 类的指针:

 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
Run Code Online (Sandbox Code Playgroud)

在我的班级中,我有一个静态比较函数:

static bool compare(ListNode* n1, ListNode* n2) {
    return n1->val < n2->val;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试像这样初始化优先级队列:

priority_queue<ListNode*, vector<ListNode*>, decltype(compare)> pq(compare);
Run Code Online (Sandbox Code Playgroud)

但我不断收到错误,说:

In file included from prog_joined.cpp:1:
In file included from ./precompiled/headers.h:55:
In …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors priority-queue

2
推荐指数
1
解决办法
167
查看次数

标签 统计

c++ ×1

compiler-errors ×1

priority-queue ×1