我正在实现Dijkstra的算法,我想使用STL的'priority_queue'来加速编码过程,但是我经常遇到这种情况,因为我试图用C++进行编码,对语言的理解正在减慢我的速度。我发现这个例子在http://www.cplusplus.com/reference/stl/priority_queue/priority_queue/,但不明白下面是这样做的:运算符重载运算符()'
// constructing priority queues
#include <iostream>
#include <queue>
using namespace std;
class mycomparison
{
bool reverse;
public:
mycomparison(const bool& revparam=false)
{reverse=revparam;}
bool operator() (const int& lhs, const int&rhs) const
{
if (reverse) return (lhs>rhs);
else return (lhs<rhs);
}
};
int main()
{
int myints[]= {10,60,50,20};
priority_queue<int> first;
priority_queue<int> second (myints,myints+4);
priority_queue< int, vector<int>, greater<int> > third (myints,myints+4);
// using mycomparison:
priority_queue< int, vector<int>, mycomparison > fourth;
typedef priority_queue<int,vector<int>,mycomparison> mypq_type;
mypq_type fifth (mycomparison());
mypq_type sixth (mycomparison(true));
return 0;
}
更具体地讲,“布尔运算符()(const int的& LHS,const int的&右)常量”是什么绊倒我。剩下的我很好。我认为它重载了一个操作符,但'operator()'对我来说没有意义。任何帮助,将不胜感激。
现在回想起来,这是一个坏主意后,我发现因为我是有只是这个问题的代码全部正在超载的运营商。 – Daeden