C++: priority_queue

转载来源: http://www.cnblogs.com/flyoung2008/articles/2136485.html

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  

 5 using namespace std;

 6  

 7 class priority_queue

 8 {

 9     private:

10         vector<int> data;

11          

12     public:

13         void push( int t ){ 

14             data.push_back(t); 

15             push_heap( data.begin(), data.end()); 

16         }

17          

18         void pop(){

19             pop_heap( data.begin(), data.end() );

20             data.pop_back();

21         }

22          

23         int top() { return data.front(); }

24         int size() { return data.size(); }

25         bool empty() { return data.empty(); }

26 };

27  

28  

29 int main()

30 {

31     priority_queue test;

32     test.push( 3 );

33     test.push( 5 );

34     test.push( 2 );

35     test.push( 4 );

36      

37     while( !test.empty() ){

38         cout << test.top() << endl;

39         test.pop(); }

40          

41     return 0;

42  

43 }
View Code

STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。 

priority_queue 对于基本类型的使用方法相对简单。他的模板声明带有三个参数:
priority_queue<Type, Container, Functional>

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

 1 #include <iostream>

 2 #include <queue>

 3  

 4 using namespace std;

 5  

 6 int main(){

 7     priority_queue<int> q;

 8      

 9     for( int i= 0; i< 10; ++i ) q.push( rand() );

10     while( !q.empty() ){

11         cout << q.top() << endl;

12         q.pop();

13     }

14      

15     getchar();

16     return 0;

17 }
View Code

如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆

 1 #include <iostream>

 2 #include <queue>

 3  

 4 using namespace std;

 5  

 6 int main(){

 7     priority_queue<int, vector<int>, greater<int> > q;

 8      

 9     for( int i= 0; i< 10; ++i ) q.push( rand() );

10     while( !q.empty() ){

11         cout << q.top() << endl;

12         q.pop();

13     }

14      

15     getchar();

16     return 0;

17 }
View Code

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

 1 #include <iostream>

 2 #include <queue>

 3  

 4 using namespace std;

 5  

 6 struct Node{

 7     int x, y;

 8     Node( int a= 0, int b= 0 ):

 9         x(a), y(b) {}

10 };

11  

12 bool operator<( Node a, Node b ){

13     if( a.x== b.x ) return a.y> b.y;

14     return a.x> b.x; 

15 }

16  

17 int main(){

18     priority_queue<Node> q;

19      

20     for( int i= 0; i< 10; ++i )

21     q.push( Node( rand(), rand() ) );

22      

23     while( !q.empty() ){

24         cout << q.top().x << ' ' << q.top().y << endl;

25         q.pop();

26     }

27      

28     getchar();

29     return 0;

30 }
View Code

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义则可以按如下方式:

 1 #include <iostream>

 2 #include <queue>

 3  

 4 using namespace std;

 5  

 6 struct Node{

 7     int x, y;

 8     Node( int a= 0, int b= 0 ):

 9         x(a), y(b) {}

10 };

11  

12 struct cmp{

13     bool operator() ( Node a, Node b ){

14         if( a.x== b.x ) return a.y> b.y;

15          

16         return a.x> b.x; }

17 };

18  

19 int main(){

20     priority_queue<Node, vector<Node>, cmp> q;

21      

22     for( int i= 0; i< 10; ++i )

23     q.push( Node( rand(), rand() ) );

24      

25     while( !q.empty() ){

26         cout << q.top().x << ' ' << q.top().y << endl;

27         q.pop();

28     }

29      

30     getchar();

31     return 0;

32 } 

33  

34  

35 //以上代码实现的是一个小顶堆
View Code

 

你可能感兴趣的:(Queue)