我重写Java代码为C++代码时遇到复制Java的优先级队列的问题..如何将java优先级队列转换为C++优先级队列?
Java的优先级队列的比较功能如下所示:
public int compareTo(Item that) {
if (this.dist < that.dist)
return -1;
if (this.dist > that.dist)
return 1;
return 0;
}
和我做了一个比较功能C++,但它无法正常工作:
struct cmp
{
bool operator()(Item it1, Item it2)
{
if(it1.dist > it2.dist)
return true;
}
};
这是我的C++代码的优先级队列
我称之为优先级队列这样
priority_queue<Item, vector<Item>, cmp> que;
但是,Java的秩序和C++的顺序不同..
帮我请
如果这还不够的示例代码,我可以提供我的整个代码。 Java和C++代码
'bool操作符()(Item it1,Item it2)'在C++中,传递const引用,而不是通过值。不要将Java用作编写C++代码的模型。 'bool operator()(const Item&it1,const Item&it2)' – PaulMcKenzie
C++版本实现[strict weak ordering](http://en.wikipedia.com/wiki/Strict_weak_ordering),所以它基本上不能处理同等优先级的值。 Java版本可以处理相同的优先级,但会以任意顺序(不是FIFO或LIFO)返回相同优先级的值,所以通常最好不要具有同等优先级的值。如果你没有相同优先级的值,那么是的,你可以转换。 – Andreas
@Andreas:'std :: priority_queue'处理相等的值就好了。 –