priority

    xiaoxiao2021-03-25  160

    优先队列(priority queue)

      优先级队列 是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。

           首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时,有一定的选择性,即根据元素的属性选择某一项值最优的出队~

     

    关于priority_queue 1,关于STL中的priority_queue:确定用top()查看顶部元素时,该元素是具有最高优先级的一个元素. 调用pop()删除之后,将促使下一个元素进入该位置.  2,如同stack和queue,priority_queue是一个基于基本序列容器进行构建的适配器,默认的序列器是vector.  

    模板原型:

    [cpp]  view plain  copy priority_queue<Type,Container,Compare>      //  Type 为数据类型    //  Container 为保存数据的容器, 必须是用数组实现的容器,比如 vector, deque 但不能用list。STL里面默认用的是 vector   //  Compare为元素比较方式。. 比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。  

     

    常用的操作如下:

    empty()  如果优先队列为空,则返回真  pop()  删除第一个元素  push()  加入一个元素  size()  返回优先队列中拥有的元素的个数  top()  返回优先队列中有最高优先级的元素 

     

    但是在使用时必须注意:priority_queue放置元素时,不会判断元素是否重复。(因为在模板的第二个参数时顺序容器,不能保证元素的唯一性)此外可以替代默认的Compare函数

     

    priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法 实 现,也算是堆的另外一种形式。

    先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue 用法相 似的 priority_queue, 以加深对 priority_queue 的理解

     

    [cpp]  view plain  copy #include<iostream>   #include<algorithm>   #include<vector>   using namespace std;      class priority_queue {          private:           vector<int> data;          public:           void push( int t ){               data.push_back(t);               push_heap( data.begin(), data.end() );           }           void pop(){               pop_heap( data.begin(), data.end() );           }           int top()    { return data.front(); }           int size()   { return data.size();  }           bool empty() { return data.empty(); }      };   // STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。         int main()   {       priority_queue test;       test.push( 3 );       test.push( 5 );       test.push( 2 );       test.push( 4 );       while( !test.empty() ){           cout << test.top() << endl;           test.pop();       }       return 0;   }  

    看例子:

    [cpp]  view plain  copy #include<iostream>   #include<queue>   using namespace std;   int main()   {       priority_queue<int> q;          for(int i=0; i<10; ++i)           q.push( rand() );          while( !q.empty() ){           cout << q.top() << endl;           q.pop();       }       getchar();          return 0;   }  

     

    如果要用到小顶堆,则一般要把模板的三个参数都带进去。

    STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆。( less< >则是从大到小) 例子:

    [cpp]  view plain  copy #include <iostream>      #include <queue>      using namespace std;          int main()   {         priority_queue<int, vector<int>, greater<int> > q;  //注意 greater<int>和后面一个'>'要有空格分开!                 forint i= 0; i< 10; ++i )            q.push( rand() );            while( !q.empty() ){             cout << q.top() << endl;             q.pop();         }                  getchar();         return 0;     }  

    对于自定义类型,则必须自己重载 operator< 或者自己写仿函数 先看看例子:

    [cpp]  view plain  copy #include<iostream>   #include<queue>   using namespace std;      struct Node{       int x;       int y;       Node(int a=0, int b=0): x(a),y(b) { }   };      bool operator < (Node a, Node b){       if(a.x == b.x)           return (a.y > b.y);       return (a.x > b.x);   }         int main()   {       priority_queue<Node>q;       for(int i=0; i<10; ++i)           q.push( Node( rand(), rand() ) );       while( !q.empty() ){           cout << q.top().x<<" "<<q.top().y<<endl;           q.pop();       }       getchar();       return 0;   }  

    自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。

    但此时不能像基本类型这样声明priority_queue<Node, vector<Node>, greater<Node> >;原因是 greater<Node> 没有定义,如果想用这种方法定义则可以按如下方式

    例子:

    [cpp]  view plain  copy #include<iostream>   #include<queue>   using namespace std;      struct Node{       int x;       int y;       Node( int a=0, int b=0 ): x(a),y(b) { }   };      struct cmp{       bool operator() ( Node a, Node b ){           if( a.x==b.x )               return a.y>b.y;           return a.x>b.x;       }   };      int main()   {       priority_queue<Node, vector<Node>,cmp> q;              for(int i=0; i<10; ++i)           q.push( Node( rand(), rand() ) );       while( !q.empty() ){           cout << q.top().x <<" "<< q.top().y << endl;           q.pop();       }          getchar();       return 0;   }  

     

    以上例子的

    [cpp]  view plain  copy struct cmp{       bool operator() ( Node a, Node b ){           if( a.x==b.x )               return a.y>b.y;           return a.x>b.x;       }   };   为重点。

     

    例2:

     

    [cpp]  view plain  copy #include<iostream>   #include<functional>   #include<cstdlib>   #include<algorithm>   #include<queue>   #include<set>   #include<vector>   #include<string>   using namespace std;      struct Node{       int data;       Node() {}       Node( int n ) { data=n; }   };      struct myCmp: binary_function< Node,Node,bool >{       bool operator () (const Node &a,const Node &b) {           return a.data < b.data;       }   };      /*  根据实践即便不从 binary_function 派生也是可以使用的    class myCmp{  public:       bool operator () (const Node& a,const Node& b){         return a.data < b.data;         }  };    */   int main()   {       priority_queue<Node,vector<Node>,myCmp> PQ;          set<Node,myCmp> s;       Node arr[6]={1,-1,2,-2,3,-3};       sort(arr,arr+6,myCmp() );          return 0;   }  

    转载请注明原文地址: https://ju.6miu.com/read-7942.html

    最新回复(0)