struct node double GHf;;signed main priority_queuenode q; bool operatorconst node &aconst node &b return aGbG;从小到大排序 报错了请修改
你在结构体 node 中定义了比较运算符 <,但是你没有将它放在合适的位置。
在 C++ 中,自定义的比较运算符应该被放置在全局作用域中,而不是在 main 函数内部。所以你需要将 bool operator<(const node &a,const node &b) 函数移到 main 函数外部。
另外,你在 priority_queue<node> q; 的定义中,使用了默认的比较函数 std::less 来进行节点的排序。但是你定义的 operator< 并不适用于 priority_queue,因为它是从小到大排序的。所以你需要使用 std::greater 来指定排序方式。
下面是修改后的代码:
#include <iostream>
#include <queue>
using namespace std;
struct node {
double G, H, f;
};
bool operator<(const node &a, const node &b) {
return a.G > b.G; // 从小到大排序
}
int main() {
priority_queue<node, vector<node>, less<node>> q; // 使用 std::greater 指定从小到大排序
return 0;
}
这样就不会报错了。注意,我还添加了 #include <iostream> 和 using namespace std; 来包含所需的头文件和使用命名空间 std
原文地址: http://www.cveoy.top/t/topic/issp 著作权归作者所有。请勿转载和采集!