如何实现c++的排序方法 带指针的priority_queue
我的优先级队列声明为:
std::priority_queue<*MyClass> queue;
class MyClass {
bool operator<( const MyClass* m ) const;
}
不对队列中的项目进行排序。
怎么了? 我不想实现不同的(比较)类。
答案摘要:
问题是,指针地址是排序的。 避免这种情况的唯一方法是使用“比较指针”的类。
现在实现为:
std::priority_queue<*MyClass, vector<*MyClass>, MyClass::CompStr > queue;
class MyClass {
struct CompStr {
bool operator()(MyClass* m1, MyClass* m2);
}
}
My priority queue declared as:
std::priority_queue<*MyClass> queue;
class MyClass {
bool operator<( const MyClass* m ) const;
}
is not sorting the items in the queue.
What is wrong? I would not like to implement a different (Compare) class.
Answer summary:
The problem is, the pointer addresses are sorted. The only way to avoid this is a class that 'compares the pointers'.
Now implemented as:
std::priority_queue<*MyClass, vector<*MyClass>, MyClass::CompStr > queue;
class MyClass {
struct CompStr {
bool operator()(MyClass* m1, MyClass* m2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为 que 提供 Compare 函子 ptr_less。
如果您希望 ptr_less 与 std 库的其余部分(活页夹、作曲家等)兼容:
否则您可以使用简化版本:
Give the que the Compare functor ptr_less.
If you want the ptr_less to be compatible with the rest of the std library (binders, composers, ... ):
Otherwise you can get away with the simplified version:
由于您的priority_queue仅包含指针值,因此它将使用指针的默认比较运算符 - 这将按地址对它们进行排序,这显然不是您想要的。 如果您更改
priority_queue
以按值存储类实例,它将使用您定义的运算符。 或者,您必须提供比较功能。Since your
priority_queue
contains only pointer values, it will use the default comparison operator for the pointers - this will sort them by address which is obviously not what you want. If you change thepriority_queue
to store the class instances by value, it will use the operator you defined. Or, you will have to provide a comparison function.您提供的运算符 <() 会将 MyClass 对象与指向 MyClass 对象的指针进行比较。 但你的队列只包含指针(我认为)。 您需要一个以两个指针作为参数的比较函数。
所有这些都是基于一些假设 - 请使用复制和粘贴来发布您的实际代码。
The operator <() you have provided will compare a MyClass object with a pointer to a MyClass object. But your queue contains only pointers (I think). You need a comparison function that takes two pointers as parameters.
All this is based on some suppositions - please post your actual code, using copy and paste.
不确定优先级队列的东西,因为我从未使用过它,但要进行直接排序,您可以这样做:
注意内存泄漏,这只是一个示例......
进一步注意:这并不是一个实现优先队列! 该向量只是使用我创建的函子通过指针比较两个对象的示例。 尽管我知道什么是优先级队列以及它的大致工作原理,但我从未使用过实现它们的 STL 功能。
更新:我认为 TimW 提出了一些有效的观点。 我不知道为什么他的票数这么低。 我认为我的答案可以改进如下:
这更干净(特别是如果您考虑使用值容器而不是指针 - 不需要进一步的工作)。
Not sure about the priority queue stuff because I've never used it but to do a straight sort, you can do this:
Note the memory leaks, this is only an example...
Further note: This is not intended to be an implementation of priority queue! The vector is simply an example of using the functor I created to compare two objects via their pointers. Although I'm aware of what a priority queue is and roughly how it works, I have never used the STL features that implement them.
Update: I think TimW makes some valid points. I don't know why he was downvoted so much. I think my answer can be improved as follows:
which is cleaner (especially if you consider having a container of values rather than pointers - no further work would be necessary).