简体   繁体   English

STL优先级队列和带指针的重载

[英]STL priority queue and overloading with pointers

This is my first time using a priority queue. 这是我第一次使用优先级队列。 I'm trying to implement Dijkstra's algorithm for school and I figured I need a min heap to do this. 我正在尝试为学校实现Dijkstra的算法,我想我需要一个最小堆才能做到这一点。 Right now my nodes are pointers and I want to compare their weight, but I don't think I can overload > and < with pointers? 现在我的节点是指针,我想比较它们的重量,但我不认为我可以重载>和<指针? Is there a way I could accomplish this? 有没有办法可以做到这一点?

Code this far: 代码到目前为止:

priority_queue<Node*, vector<Node*>, node_comparison> minHeap;

And then I have a struct to compare the node's weights 然后我有一个结构来比较节点的权重

struct node_comparison 
{
   bool operator<( const Node* a, const Node* b ) const 
   {
    return a->totalWeight < b->totalWeight;
   }
};

However it says there are too many parameters for this operator function. 但是它说这个操作员功能的参数太多了。 I've been trying to figure out how I could manage a min heap priority queue with my nodes for a while now and keep getting stuck. 我一直试图找出如何用我的节点管理一个最小堆优先级队列一段时间,然后继续卡住。 Any ideas? 有任何想法吗?

If I understand your question correctly, I believe what you actually want is to make node_comparison a functor (more specifically, a binary predicate): 如果我正确理解你的问题,我相信你真正想要的是使node_comparison成为一个函子 (更具体地说,是一个二元谓词):

struct node_comparison 
{
    bool operator () ( const Node* a, const Node* b ) const 
    {
        return a->totalWeight < b->totalWeight;
    }
};

A functor is a class whose objects provide an overload of the call operator ( operator () ) and, therefore, can be invoked with the same syntax you would use for invoking a function: 仿函数是一个类,其对象提供了调用操作符( operator () )的重载,因此可以使用与调用函数相同的语法来调用:

Node* p1 = ...;
Node* p2 = ...;
node_comparison comp;
bool res = comp(p1, p2) // <== Invokes your overload of operator ()

Internally, std::priority_queue will instantiate your predicate more or less like I did in the code snippet above, and invoke it that way to perform comparisons between its elements. 在内部, std::priority_queue将像我在上面的代码片段中那样或多或少地实例化你的谓词,并以这种方式调用它来执行其元素之间的比较。


The advantage of functors over regular functions is that they could hold state information (something you probably won't need for the moment, but which often turns out to be desirable): 仿函数优于常规函数的优点是它们可以保存状态信息(目前您可能不需要这些信息,但通常证明是理想的):

#include <cmath>

struct my_comparator
{
    my_comparator(int x) : _x(x) { }

    bool operator () (int n, int m) const
    {
        return abs(n - _x) < abs(m - _x);
    }

    int _x;
};

The above predicate, for instance, compares integers based on how distant they are from another integer provided at construction time. 例如,上述谓词基于它们与构造时提供的另一个整数的距离来比较整数。 This is how it could be used: 这是如何使用它:

#include <queue>
#include <iostream>

void foo(int pivot)
{
    my_comparator mc(pivot);
    std::priority_queue<int, std::deque<int>, my_comparator> pq(mc);

    pq.push(9);
    pq.push(2);
    pq.push(17);

    while (!pq.empty())
    {
        std::cout << pq.top();
        pq.pop();
    }
}

int main()
{
    foo(7);

    std::cout << std::endl;

    foo(10);
}

You would need your comparison functor to implement bool operator()(....) , not bool operator<(....) : 你需要你的比较函子来实现bool operator()(....) ,而不是bool operator<(....)

struct node_comparison 
{
   bool operator()( const Node* a, const Node* b ) const 
   {
    return a->totalWeight < b->totalWeight;
   }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM