如何实现c++的排序方法 带指针的priority_queue

发布于 2024-07-23 15:35:23 字数 516 浏览 8 评论 0原文

我的优先级队列声明为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

临风闻羌笛 2024-07-30 15:35:23

为 que 提供 Compare 函子 ptr_less。

如果您希望 ptr_less 与 std 库的其余部分(活页夹、作曲家等)兼容:

template<class T>
struct ptr_less
    : public binary_function<T, T, bool> {  
        bool operator()(const T& left, const T& right) const{
            return ((*left) <( *right));
        }
};

std::priority_queue<MyClass*, vector<MyClass*>, ptr_less<MyClass*> > que; 

否则您可以使用简化版本:

struct ptr_less {
    template<class T>
    bool operator()(const T& left, const T& right) const {
        return ((*left) <( *right));
    }
};

std::priority_queue<MyClass*, vector<MyClass*>, ptr_less > que; 

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, ... ):

template<class T>
struct ptr_less
    : public binary_function<T, T, bool> {  
        bool operator()(const T& left, const T& right) const{
            return ((*left) <( *right));
        }
};

std::priority_queue<MyClass*, vector<MyClass*>, ptr_less<MyClass*> > que; 

Otherwise you can get away with the simplified version:

struct ptr_less {
    template<class T>
    bool operator()(const T& left, const T& right) const {
        return ((*left) <( *right));
    }
};

std::priority_queue<MyClass*, vector<MyClass*>, ptr_less > que; 
稍尽春風 2024-07-30 15:35:23

由于您的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 the priority_queue to store the class instances by value, it will use the operator you defined. Or, you will have to provide a comparison function.

疾风者 2024-07-30 15:35:23

您提供的运算符 <() 会将 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.

孤凫 2024-07-30 15:35:23

不确定优先级队列的东西,因为我从未使用过它,但要进行直接排序,您可以这样做:

class A
{
    friend struct ComparePtrToA;
public:
    A( int v=0 ):a(v){}
private:
    int a;
};

struct ComparePtrToA
{
    bool operator()(A* a1, A* a2) {return a1->a < a2->a;}
};

#include <vector>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
    vector<A*> someAs;
    someAs.push_back(new A(1));
    someAs.push_back(new A(3));
    someAs.push_back(new A(2));
    sort( someAs.begin(), someAs.end(), ComparePtrToA() );
}

注意内存泄漏,这只是一个示例......

进一步注意:这并不是一个实现优先队列! 该向量只是使用我创建的函子通过指针比较两个对象的示例。 尽管我知道什么是优先级队列以及它的大致工作原理,但我从未使用过实现它们的 STL 功能。

更新:我认为 TimW 提出了一些有效的观点。 我不知道为什么他的票数这么低。 我认为我的答案可以改进如下:

class A
{
public:
    A( int v=0 ):a(v){}
    bool operator<( const A& rhs ) { return a < rhs.a; }
private:
    int a;
};

struct ComparePtrToA
{
    bool operator()(A* a1, A* a2) {return *a1 < *a2;}
};

这更干净(特别是如果您考虑使用值容器而不是指针 - 不需要进一步的工作)。

Not sure about the priority queue stuff because I've never used it but to do a straight sort, you can do this:

class A
{
    friend struct ComparePtrToA;
public:
    A( int v=0 ):a(v){}
private:
    int a;
};

struct ComparePtrToA
{
    bool operator()(A* a1, A* a2) {return a1->a < a2->a;}
};

#include <vector>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
    vector<A*> someAs;
    someAs.push_back(new A(1));
    someAs.push_back(new A(3));
    someAs.push_back(new A(2));
    sort( someAs.begin(), someAs.end(), ComparePtrToA() );
}

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:

class A
{
public:
    A( int v=0 ):a(v){}
    bool operator<( const A& rhs ) { return a < rhs.a; }
private:
    int a;
};

struct ComparePtrToA
{
    bool operator()(A* a1, A* a2) {return *a1 < *a2;}
};

which is cleaner (especially if you consider having a container of values rather than pointers - no further work would be necessary).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文