C++ 优先级队列<&燃气轮机;指针的比较?

C++ 优先级队列<&燃气轮机;指针的比较?,c++,templates,stl,C++,Templates,Stl,所以我使用STL优先级队列和指针。。。我不想使用值类型,因为创建一堆新对象只是为了在优先级队列中使用是非常浪费的。所以我正在尝试这样做: class Int { public: Int(int val) : m_val(val) {} int getVal() { return m_val; } private: int m_val; } priority_queue<Int*> myQ; myQ.push(new Int(5)); myQ.push(n

所以我使用STL优先级队列和指针。。。我不想使用值类型,因为创建一堆新对象只是为了在优先级队列中使用是非常浪费的。所以我正在尝试这样做:

class Int {
public:
    Int(int val) : m_val(val) {}
    int getVal() { return m_val; }
private:
    int m_val;
}


priority_queue<Int*> myQ;

myQ.push(new Int(5));
myQ.push(new Int(6));
myQ.push(new Int(3));
class Int{
公众:
Int(Int val):m_val(val){}
int getVal(){return m_val;}
私人:
国际货币基金组织;
}
优先级队列myQ;
myQ.push(新整数(5));
myQ.push(新Int(6));
myQ.push(新Int(3));
现在,我如何编写一个比较函数,使它们在Q中正确排序?或者,有人能提出一个替代策略吗?我确实需要priority_queue接口,不想使用复制构造函数(因为数据量很大)。谢谢


编辑:Int只是一个占位符/示例。。。我知道我可以在C/C++lol中使用
int

一个整数与32位系统上的指针大小相同。在64位系统上,指针将是原来的两倍大。因此,使用正则整数更简单/更快/更好。

一个肯定有效的选项是将
Int*
替换为
shared\u ptr
,然后实现
操作符getVal();
}

您可以明确指定队列应该使用哪个比较器

#include <iostream>
#include <sstream>
#include <functional>
#include <vector>
#include <queue>

class Int {
public:
    Int(int val) : m_val(val) {}
    int getVal() { return m_val; }
    bool operator<(const Int &other) const { return m_val < other.m_val; }
private:
    int m_val;
};

template<typename Type, typename Compare = std::less<Type> >
struct pless : public std::binary_function<Type *, Type *, bool> {
    bool operator()(const Type *x, const Type *y) const
        { return Compare()(*x, *y); }
};

int main(int argc, char *argv[]) {
    std::priority_queue<Int*, std::vector<Int*>, pless<Int> > myQ;

    for (int i = 1; i < argc; i++) {
        std::stringstream ss(argv[i]);
        int x;
        ss >> x;
        myQ.push(new Int(x));
    }

    for (; !myQ.empty(); delete myQ.top(), myQ.pop())
        std::cout << myQ.top()->getVal() << std::endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
类Int{
公众:
Int(Int val):m_val(val){}
int getVal(){return m_val;}
布尔算子>x;
myQ.push(新Int(x));
}
对于(;!myQ.empty();删除myQ.top(),myQ.pop())

std::cout getVal()Int只是一个更大类的占位符。在这种情况下,忽略此响应。我正在尝试这种方法。。。而不是仅仅使用一个基本的包装类来使用共享的\u ptr。@Polaris878,尽管共享指针在这里是不相关的。你从来没有说过你想要共享所有权。
#include <iostream>
#include <sstream>
#include <functional>
#include <vector>
#include <queue>

class Int {
public:
    Int(int val) : m_val(val) {}
    int getVal() { return m_val; }
    bool operator<(const Int &other) const { return m_val < other.m_val; }
private:
    int m_val;
};

template<typename Type, typename Compare = std::less<Type> >
struct pless : public std::binary_function<Type *, Type *, bool> {
    bool operator()(const Type *x, const Type *y) const
        { return Compare()(*x, *y); }
};

int main(int argc, char *argv[]) {
    std::priority_queue<Int*, std::vector<Int*>, pless<Int> > myQ;

    for (int i = 1; i < argc; i++) {
        std::stringstream ss(argv[i]);
        int x;
        ss >> x;
        myQ.push(new Int(x));
    }

    for (; !myQ.empty(); delete myQ.top(), myQ.pop())
        std::cout << myQ.top()->getVal() << std::endl;

    return 0;
}