【发布时间】:2013-06-22 18:16:41
【问题描述】:
我查看了这个thread,它谈到了使用这种方法进行比较:
struct thing
{
int a;
char b;
bool operator<(const thing &o) const
{
return a < o.a;
}
};
priority_queue<thing> pq;
另一方面,其他使用方法如下:
struct Time {
int h;
int m;
int s;
};
class CompareTime {
public:
bool operator()(Time& t1, Time& t2) // Returns true if t1 is earlier than t2
{
if (t1.h < t2.h) return true;
if (t1.h == t2.h && t1.m < t2.m) return true;
if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
return false;
}
}
priority_queue<Time, vector<Time>, CompareTime> pq;
虽然我对第一种方法有逻辑,但我并没有完全理解第二种方法。主要是因为语法。我不确定重载运算符operator() 的含义。那个运算符重载是什么?
还有,从cplusplus上priority_queue,下面的我不太明白,主要是第二个参数。
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
换句话说,我不明白第二种方法及其调用约定。
另外,有什么区别,首选哪种方法?
【问题讨论】:
标签: syntax struct priority-queue