priority_queue(优先队列)用法总结

优先队列(priority queue)

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

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

 

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

  1. priority_queue<Type,Container,Compare>    
  2.     
  3. //  Type 为数据类型     
  4. //  Container 为保存数据的容器, 必须是用数组实现的容器,比如 vector, deque 但不能用list。STL里面默认用的是 vector    
  5. //  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 的理解

 

  1. #include<iostream>    
  2. #include<algorithm>    
  3. #include<vector>    
  4. using namespace std;    
  5.     
  6. class priority_queue {    
  7.     
  8.     private:    
  9.         vector<int> data;    
  10.     
  11.     public:    
  12.         void push( int t ){    
  13.             data.push_back(t);    
  14.             push_heap( data.begin(), data.end() );    
  15.         }    
  16.         void pop(){    
  17.             pop_heap( data.begin(), data.end() );    
  18.         }    
  19.         int top()    { return data.front(); }    
  20.         int size()   { return data.size();  }    
  21.         bool empty() { return data.empty(); }    
  22.     
  23. };    
  24. // STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。    
  25.     
  26.     
  27. int main()    
  28. {    
  29.     priority_queue test;    
  30.     test.push( 3 );    
  31.     test.push( 5 );    
  32.     test.push( 2 );    
  33.     test.push( 4 );    
  34.     while( !test.empty() ){    
  35.         cout << test.top() << endl;    
  36.         test.pop();    
  37.     }    
  38.     return 0;    
  39. }    
看例子:

  1. #include<iostream>    
  2. #include<queue>    
  3. using namespace std;    
  4. int main()    
  5. {    
  6.     priority_queue<int> q;    
  7.     
  8.     for(int i=0; i<10; ++i)    
  9.         q.push( rand() );    
  10.     
  11.     while( !q.empty() ){    
  12.         cout << q.top() << endl;    
  13.         q.pop();    
  14.     }    
  15.     getchar();    
  16.     
  17.     return 0;    
  18. }    



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

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

  1. #include <iostream>       
  2. #include <queue>       
  3. using namespace std;      
  4.       
  5. int main()    
  6. {      
  7.     priority_queue<int, vector<int>, greater<int> > q;  //注意 greater<int>和后面一个'>'要有空格分开!     
  8.           
  9.     forint i= 0; i< 10; ++i )     
  10.         q.push( rand() );      
  11.     
  12.     while( !q.empty() ){      
  13.         cout << q.top() << endl;      
  14.         q.pop();      
  15.     }      
  16.           
  17.     getchar();      
  18.     return 0;      
  19. }    

对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
先看看例子:
  1. #include<iostream>    
  2. #include<queue>    
  3. using namespace std;    
  4.     
  5. struct Node{    
  6.     int x;    
  7.     int y;    
  8.     Node(int a=0, int b=0): x(a),y(b) { }    
  9. };    
  10.     
  11. bool operator < (Node a, Node b){    
  12.     if(a.x == b.x)    
  13.         return (a.y > b.y);    
  14.     return (a.x > b.x);    
  15. }    
  16.     
  17.     
  18. int main()    
  19. {    
  20.     priority_queue<Node>q;    
  21.     for(int i=0; i<10; ++i)    
  22.         q.push( Node( rand(), rand() ) );    
  23.     while( !q.empty() ){    
  24.         cout << q.top().x<<" "<<q.top().y<<endl;    
  25.         q.pop();    
  26.     }    
  27.     getchar();    
  28.     return 0;    
  29. }    

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

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

例子:

  1. #include<iostream>    
  2. #include<queue>    
  3. using namespace std;    
  4.     
  5. struct Node{    
  6.     int x;    
  7.     int y;    
  8.     Node( int a=0, int b=0 ): x(a),y(b) { }    
  9. };    
  10.     
  11. struct cmp{    
  12.     bool operator() ( Node a, Node b ){    
  13.         if( a.x==b.x )    
  14.             return a.y>b.y;    
  15.         return a.x>b.x;    
  16.     }    
  17. };    
  18.     
  19. int main()    
  20. {    
  21.     priority_queue<Node, vector<Node>,cmp> q;    
  22.         
  23.     for(int i=0; i<10; ++i)    
  24.         q.push( Node( rand(), rand() ) );    
  25.     while( !q.empty() ){    
  26.         cout << q.top().x <<" "<< q.top().y << endl;    
  27.         q.pop();    
  28.     }    
  29.     
  30.     getchar();    
  31.     return 0;    
  32. }    

以上例子的
  1. struct cmp{    
  2.     bool operator() ( Node a, Node b ){    
  3.         if( a.x==b.x )    
  4.             return a.y>b.y;    
  5.         return a.x>b.x;    
  6.     }    
  7. };    

为重点。

 

例2:

  1. #include<iostream>    
  2. #include<functional>    
  3. #include<cstdlib>    
  4. #include<algorithm>    
  5. #include<queue>    
  6. #include<set>    
  7. #include<vector>    
  8. #include<string>    
  9. using namespace std;    
  10.     
  11. struct Node{    
  12.     int data;    
  13.     Node() {}    
  14.     Node( int n ) { data=n; }    
  15. };    
  16.     
  17. struct myCmp: binary_function< Node,Node,bool >{    
  18.     bool operator () (const Node &a,const Node &b) {    
  19.         return a.data < b.data;    
  20.     }    
  21. };    
  22.     
  23. /*  
  24. 根据实践即便不从 binary_function 派生也是可以使用的  
  25.   
  26. class myCmp{  
  27. public:   
  28.     bool operator () (const Node& a,const Node& b){  
  29.        return a.data < b.data;     
  30.     }  
  31. };  
  32.   
  33. */    
  34. int main()    
  35. {    
  36.     priority_queue<Node,vector<Node>,myCmp> PQ;    
  37.     
  38.     set<Node,myCmp> s;    
  39.     Node arr[6]={1,-1,2,-2,3,-3};    
  40.     sort(arr,arr+6,myCmp() );    
  41.     
  42.     return 0;    
  43. }   

猜你喜欢

转载自blog.csdn.net/qq_41660465/article/details/79370470