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;
換句話說,我不明白第二種方法及其調用約定。
此外,有什麼區別,哪種方法是首選?