0
我已经看过移到该thread其中谈到使用这种方法用于比较:C++ STL priority_queue与结构Clearification
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 on priority_queue,我不太了解以下内容,主要是第二个参数。
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
换句话说,我不明白第二种方法及其调用约定。
此外,有什么区别,哪种方法是首选?