gpt4 book ai didi

c++ - 优先级队列 C++ 中的 "No matching member function for call to push"

转载 作者:行者123 更新时间:2023-12-02 03:30:54 29 4
gpt4 key购买 nike

我现在正在尝试实现优先级队列,但我不断收到错误消息,提示 push 不是成员函数。我以前从未使用过优先级队列,所以我很确定我在某个地方搞砸了......

struct X
{
unordered_map<string, double> distance;
unordered_map<string, string> vertices;
};

void make(std::string source)
{
X distance;
X parent;
priority_queue<X, std::vector<X>, greater<void>> pq; //i'm pretty sure there's something wrong with the templates?
distance.distance[source] = 0;
pq.push(std::make_pair(start, 0));

最佳答案

该类必须具有可比性,并且必须响应 < 和 > 等运算符,以便队列可以对它们进行排序:

class X {
public:
unordered_map<string, double> distance;
unordered_map<string, string> vertices;
int priority;
bool operator< (const X& b) {
return priority < b.priority;
}
bool operator> (const X& b) {
return priority > b.priority;
}
};

void make(std::string source) {
priority_queue<X, vector<X>, greater<void>> pq;

X element;
element.distance[source] = 0;
element.priority = 100;
pq.push(element);
}

关于c++ - 优先级队列 C++ 中的 "No matching member function for call to push",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708037/

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