C++ priority_queue

  1. 头文件

    #include 
    
  2. 自定义比较函数

    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, 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, cmp> q();
      
    3. 重载操作符
      struct node  
      {  
          friend bool operator< (node n1, node n2)  
          {  
              return n1.priority < n2.priority;  
          }  
          int priority;  
          int value;  
      };  
      
  3. 排列顺序
    使用<时大的内容具有较高优先级

Reference

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

你可能感兴趣的:(C++ priority_queue)