優先隊列

#include#include#include#include#includeusing namespace std;//定義比較結構struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值優先 }};struct cmp2{ bool operator ()(int &a,int &b){ return aa.x;//最小值優先 }};struct number2{ int x; bool operator < (const number2 &a) const { return xque1;//最小值優先 priority_queueque2;//最大值優先 priority_queueque3;//注意“>>”會被認爲錯誤, priority_queueque4;////最大值優先 priority_queueque5; //最小優先級隊列 priority_queueque6; //最大優先級隊列 int i; for(i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(i=0;num1[i].x;i++) que5.push(num1[i]); for(i=0;num2[i].x;i++) que6.push(num2[i]); printf("採用默認優先關係:/n(priority_queueque;)/n"); printf("Queue 0:/n"); while(!que.empty()){ printf("%3d",que.top()); que.pop(); } puts(""); puts(""); printf("採用結構體自定義優先級方式一:/n(priority_queueque;)/n"); printf("Queue 1:/n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } puts(""); printf("Queue 2:/n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } puts(""); puts(""); printf("採用頭文件\"functional\"內定義優先級:/n(priority_queueque;)/n"); printf("Queue 3:/n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } puts(""); printf("Queue 4:/n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } puts(""); puts(""); printf("採用結構體自定義優先級方式二:/n(priority_queueque)/n"); printf("Queue 5:/n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } puts(""); printf("Queue 6:/n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } puts(""); return 0;}/*運行結果 :採用默認優先關係:(priority_queueque;)Queue 0:83 72 56 47 36 22 14 10 7 3採用結構體自定義優先級方式一:(priority_queueque;)Queue 1: 7 10 14 22 36 47 56 72 83 91Queue 2:83 72 56 47 36 22 14 10 7 3採用頭文件"functional"內定義優先級:(priority_queueque;)Queue 3: 7 10 14 22 36 47 56 72 83 91Queue 4:83 72 56 47 36 22 14 10 7 3採用結構體自定義優先級方式二:(priority_queueque)Queue 5: 7 10 14 22 36 47 56 72 83 91Queue 6:83 72 56 47 36 22 14 10 7 3*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章