【问题标题】:Dijkstras algorithm with priority_queue具有优先队列的 Dijkstra 算法
【发布时间】:2013-08-08 04:06:57
【问题描述】:

我正在尝试实现 Dijkstra 算法。我正在使用这个priority_queue

priority_queue<pair<PathInfo,string>,vector<pair<PathInfo,string> >,QueueComp> p;

在哪里

class QueueComp{
    PathComp* pc;
public:
    QueueComp(PathComp*);
    bool operator ()(const pair<PathInfo,string>&,const pair<PathInfo,string>& );
};

是我的“比较”功能。错误是 QueueComp 没有默认构造函数,我不允许创建一个。我该怎么做才能使我的代码编译?顺便说一句,这是错误

error: no matching function for call to 'QueueComp::QueueComp()'

这是路径comp.h

class PathComp{
public:
   virtual bool betterThan(const PathInfo& path1,const PathInfo& path2)=0;
};

这是路径comppl.h

#include "pathcomp.h"

class PathCompPL:public PathComp{
public:
virtual bool betterThan(const PathInfo& path1,const PathInfo& path2);
};

这是路径comppl.cpp

#include "pathcomppl.h"

bool PathCompPL::betterThan(const PathInfo& path1,const PathInfo& path2){
    if (path1.getTotalPrice()>path2.getTotalPrice())
        return true;

    if (path1.getTotalPrice()==path2.getTotalPrice() && path1.getTotalLength()>path2.getTotalLength())
        return true;

    return false;
}

扩展的错误信息

main.cpp: In constructor ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = std::pair<PathInfo, std::basic_string<char> >; _Sequence = std::vector<std::pair<PathInfo, std::basic_string<char> > >; _Compare = QueueComp]’:
main.cpp:11:87: error: no matching function for call to ‘QueueComp::QueueComp()’
main.cpp:11:87: note: candidates are:
In file included from main.cpp:5:0:
queuecomp.h:14:5: note: QueueComp::QueueComp(PathComp*)
queuecomp.h:14:5: note:   candidate expects 1 argument, 0 provided
queuecomp.h:10:7: note: QueueComp::QueueComp(const QueueComp&)
queuecomp.h:10:7: note:   candidate expects 1 argument, 0 provided

【问题讨论】:

  • 我认为您的 QueueComp 类型需要一个默认构造函数。
  • 好像是这样..但我不能使用一个。
  • 您想将边权重存储在优先级队列中,以便您始终可以选择最短边
  • @Slazer 不允许使用默认 ctor 的原因是什么?这是课堂作业吗?教授把它作为限制?
  • 另外,PathComp 是什么? “比较”似乎暗示它是一个比较器。也许您可以直接将其用作priority_queue 模板的第三个参数。

标签: c++ priority-queue path-finding dijkstra


【解决方案1】:

您没有默认构造函数,因为您应该初始化名为 pc 的变量。你有这个构造函数:

QueueComp(PathComp*);

您必须实现它,以便 pc 与参数相关联。

关于您的第二个问题:第一个元素是您的下一个优先级,第二个元素是较低优先级的集合,第三个是队列比较。希望对您有所帮助。

【讨论】:

  • 我实现了 QueueComp(PathComp*_pc) 将 _pc 分配给 pc。但是,priority_queue 的第三个模板参数应该如何编译呢?
  • 如果您使用 new 运算符调用我的答案中提到的构造函数,那么您将拥有一个 QueueComp 对象。该对象将是您的priority_queue 的第三个参数。所以,稍微练习一下。首先,尝试调用构造函数。如果成功,请将构造函数调用的结果分配给 QueueComp 变量。如果你成功了,那么你可以在你的priority_queue 中使用那个变量。如果您已经知道如何操作,一切都很简单。
【解决方案2】:

看来您的问题在于实施适当的比较器。您可能会考虑的一种替代方法是创建如下比较器

struct CompareEdgeWeights : public binary_function<PathInfo*, PathInfo*, bool>
    {
        bool operator()(const PathInfo* left, const PathInfo* right) const
        {
            return left->getEdgeWeight() > right->getEdgeWeight();
        }
    }; // end struct

// Priority queue of node edges
priority_queue<PathInfo*,vector<PathInfo*>,CompareEdgeWeights > * edgePriorityQueue;

让这个结构继承自 binary_function 并重载操作符 ()。然后,您可以将其用作比较器,以保持边缘从最低到最高权重值排序。注意:您可能需要稍微调整一下以符合您的实现。如果不了解更多实施情况,很难给出 100% 正确的建议。

【讨论】:

  • 我在我的问题中添加了更多实现。
【解决方案3】:

由于您有非默认构造函数,因此您需要使用附加参数初始化优先级队列。

priority_queue&lt;pair&lt;PathInfo,string&gt;,vector&lt;pair&lt;PathInfo,string&gt; &gt;,QueueComp&gt; p(QueueComp(ptrToPathCompObject));

附加参数 (QueueComp(ptrToPathCompObject)) 应该可以解决您的问题。

我假设您已经在 QueueComp 类中实现了operator()

【讨论】:

  • 很高兴它对您有所帮助。干杯!
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多