C++ priority_queue

  1. 頭文件

    #include <queue>
    
  2. 自定義比較函數(shù)

    1. lambda (c++11)
      注意使用關鍵字decltype
      auto comp = [origin]( Point a, Point b ) {
          auto aDis = distanceSquare(a, origin);
          auto bDis = distanceSquare(b, origin);
          if (aDis == bDis) {
              if (a.x == b.x) {
                  return a.y < b.y;
              } else {
                  return a.x < b.x;
              }
          } else {
              return aDis < bDis;
          }
      };
      priority_queue<Point, vector<Point>, decltype(comp)> q(comp);
      
    2. 結構體 TBD
       struct cmp{
              bool operator() ( Point a, Point b ){
                  if( a.x== b.x ) return a.y> b.y;
                  return a.x> b.x; 
              }
      };
      priority_queue<Point, vector<Point>, cmp> q();
      
    3. 重載操作符
      struct node  
      {  
          friend bool operator< (node n1, node n2)  
          {  
              return n1.priority < n2.priority;  
          }  
          int priority;  
          int value;  
      };  
      
  3. 排列順序
    使用<時大的內(nèi)容具有較高優(yōu)先級

Reference

http://blog.csdn.net/yuanjilai/article/details/8043157
https://stackoverflow.com/questions/5807735/c-priority-queue-with-lambda-comparator-error

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容