C++中priority_queue的比較函數重載的兩種方式

1.定義比較函數,以函數對象形式

    這種方式使用時,需要把函數加入priority_queue的聲明中去

 struct com{

  bool operator()( T &t1, T &t2)

    {

      if(t1.x != t2.x)

             return t1.x < t2.x -->按x降序

     return t1.y > t2.y   -->x相等時按y升序

 }

};

priority_queue<T, vector<T>, com>  que;

2.在結構體中重載<操作符(重載爲友元函數)

class T{


public:

friend bool operator<(const T &t);

};

priority<T, vector<T> ...

發佈了73 篇原創文章 · 獲贊 18 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章