優先隊列priority_queue 使用方法

優先隊列(priority_queue)

  優先級隊列 是不同於先進先出隊列的另一種隊列。每次從隊列中取出的是具有最高優先權的元素。

       首先它是一個隊列,但是它強調了“優先”二字,所以,已經不能算是一般意義上的隊列了,它的“優先”意指取隊首元素時,有一定的選擇性,即根據元素的屬性選擇某一項值最優的出隊~

 

關於priority_queue
1,關於STL中的priority_queue:確定用top()查看頂部元素時,該元素是具有最高優先級的一個元素. 調用pop()刪除之後,將促使下一個元素進入該位置. 
2,如同stack和queue,priority_queue是一個基於基本序列容器進行構建的適配器,默認的序列器是vector.
 

模板原型:

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 的理解

 

#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;
}



看例子:

#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< >則是從大到小)
例子:

#include <iostream>   
#include <queue>   
using namespace std;  
  
int main()
{  
    priority_queue<int, vector<int>, greater<int> > q;  //注意 greater<int>和後面一個'>'要有空格分開! 
      
    for( int i= 0; i< 10; ++i ) 
		q.push( rand() );  

    while( !q.empty() ){  
        cout << q.top() << endl;  
        q.pop();  
    }  
      
    getchar();  
    return 0;  
}


對於自定義類型,則必須自己重載 operator< 或者自己寫仿函數
先看看例子:

#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> 沒有定義,如果想用這種方法定義則可以按如下方式

例子:

#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;
}


 

以上例子的

struct cmp{
	bool operator() ( Node a, Node b ){
		if( a.x==b.x )
			return a.y>b.y;
		return a.x>b.x;
	}
};
爲重點。

 

例2:

 

#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;
}



 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章