C++ 自定义类上的 STL 优先级队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1541560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
STL Priority Queue on custom class
提问by bmalicoat
I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code:
我在让我的优先队列识别它应该按哪个参数排序时遇到了很多麻烦。我在自定义类中重载了小于运算符,但它似乎没有使用它。这是相关的代码:
Node.h
节点.h
class Node
{
public:
Node(...);
~Node();
bool operator<(Node &aNode);
...
}
Node.cpp
节点.cpp
#include "Node.h"
bool Node::operator<(Node &aNode)
{
return (this->getTotalCost() < aNode.getTotalCost());
}
getTotalCost() returns an int
getTotalCost() 返回一个 int
main.cpp
主程序
priority_queue<Node*, vector<Node*>,less<vector<Node*>::value_type> > nodesToCheck;
What am I missing and/or doing wrong?
我错过了什么和/或做错了什么?
回答by rlbond
less<vector<Node*>::value_type>
Means that your comparator compares the pointersto each other, meaning your vector will be sorted by the layout in memory of the nodes.
less<vector<Node*>::value_type>
意味着您的比较器将指针相互比较,这意味着您的向量将按节点内存中的布局进行排序。
You want to do something like this:
你想做这样的事情:
#include <functional>
struct DereferenceCompareNode : public std::binary_function<Node*, Node*, bool>
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return lhs->getTotalCost() < rhs->getTotalCost();
}
};
// later...
priority_queue<Node*, vector<Node*>, DereferenceCompareNode> nodesToCheck;
Note that you need to be const-correct in your definition of totalCost
.
请注意,您需要在totalCost
.
EDIT: Now that C++11 is here, you don't need to inherit from std::binary_function anymore (which means you don't need to #include functional)
编辑:既然 C++11 在这里,你不需要再从 std::binary_function 继承(这意味着你不需要 #include 函数)
回答by GManNickG
You need to make your parameter const
, because as of now you're giving it a non-cost reference, which means you might modify the object you're comparing with. (Which you aren't, and probably shouldn't).
您需要创建参数const
,因为到目前为止您正在为其提供一个非成本参考,这意味着您可能会修改要与之比较的对象。(你不是,而且可能不应该)。
You're not being const-correct. Your operator<
doesn't make modifications to the Node, so the function should be const:
你不是常量正确的。您operator<
不会对节点进行修改,因此该函数应为常量:
bool operator<(const Node &aNode) const;
After that, if you have trouble calling the getTotalCost()
function, it's likely that it is not const as well. Mark it as const if it's not already:
之后,如果您在调用该getTotalCost()
函数时遇到问题,很可能它也不是 const。如果尚未将其标记为 const:
int getTotalCost(void) const;
Your code is now (more) const-correct.
您的代码现在(更多)const 正确。
On a side note, binary operators are usually implemented outside the class:
附带说明一下,二元运算符通常在类之外实现:
class Node
{
public:
// ...
int getTotalCost(void) const;
// ...
};
bool operator<(const Node& lhs, const Node& rhs)
{
return lhs.getTotalCost() < rhs.getTotalCost();
}