相关疑难解决方法(0)

用户定义类型的优先级队列

我有以下结构

struct node{
   float val;
   int count;

}
Run Code Online (Sandbox Code Playgroud)

我有这个结构的几个对象.现在,我想将这些对象插入到STL的优先级队列中,以便优先级队列按计数对项目进行排序.有关如何这样做的任何想法?优选地,最小堆是优选的.我知道如何对原始数据类型而不是结构进行上述操作

c++ priority-queue min-heap

11
推荐指数
3
解决办法
3万
查看次数

priority_queue <>指针的比较?

所以我正在使用带有指针的STL priority_queue <> ...我不想使用值类型,因为创建一堆刚刚用于优先级队列的新对象将非常浪费.所以...我正在尝试这样做:

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));
Run Code Online (Sandbox Code Playgroud)

现在我如何编写一个比较函数来获得在Q中正确排序的函数?或者,有人可以提出替代策略吗?我真的需要priority_queue接口,并且不想使用复制构造函数(因为有大量数据).谢谢

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

c++ templates stl

4
推荐指数
1
解决办法
6422
查看次数

boost :: heap :: priority_queue vs std :: priority_queue的比较器

我正在尝试为自定义Edge类设置优先级队列,其中Edges将按权重进行比较.

class Edge 
{
public:
    int index;
    double weight;
std::pair<int, int> vertices;

Edge(int i, double w, int start, int end)
{
    index = i;
    weight = w;
    vertices.first = start;
    vertices.second = end;
}
}; 
Run Code Online (Sandbox Code Playgroud)

我成功地在自定义类上实现了一个std::priority_queue使用STL优先级队列,并使用http://gigi.nullneuron.net/comp/cpp-stl-priority-queue.php作为参考,使用这样的比较器,

struct EdgeCompare
{
    bool operator()(const Edge &e1, const Edge &e2) const
    {
        return e1.weight < e2.weight;
    }
}

std::priority_queue<Edge, std::vector<Edge>, EdgeCompare> queue;
Run Code Online (Sandbox Code Playgroud)

但是,我意识到这std::priority_queue不提供迭代器.这是我真正想要的功能,所以我决定切换到boost::heap::priority_queue.我知道boost::heap::priority_queue有一个构造函数将设置自定义比较器.但是,我找不到任何解释如何最好地传递函数的示例.我显然不能使用我用过的相同语法std::priority_queue.

我试过了

EdgeCompare comparator;
boost::heap::priority_queue<Edge> queue2(comparator.operator); …
Run Code Online (Sandbox Code Playgroud)

c++ boost std

2
推荐指数
1
解决办法
2191
查看次数

标签 统计

c++ ×3

boost ×1

min-heap ×1

priority-queue ×1

std ×1

stl ×1

templates ×1