總會忘記,如果寫一個比較函數 left < right, 那么,結果是降序還是升序呢。priority_queue是大根堆還是小根堆呢。
所以就寫了個測試。
結果表明,如果是 return left < right; 則排序是升序。priority_queue 是大根堆。
如果是 return left > right; 則排序是降序。priority_queue 是小根堆。
std::sort 底層是用快排+堆排+插入(分情況選擇用什么排序)實現,平均復雜度為 Nlog(N);
class testless{ public: bool operator ()(const int& a,const int& b)const { return a<b; } }; class testgreat{ public: bool operator ()(const int& a,const int& b)const { return a>b; } }; bool mycomparegreat(const int& a,const int& b){ return a>b; } bool mycompareless(const int& a,const int& b){ return a<b; }