zz STL 优先队列
生活随笔
收集整理的這篇文章主要介紹了
zz STL 优先队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1: #include<stdio.h> 2: #include<functional> 3: #include<queue> 4: #include<vector> 5: using namespace std; 6: //定義結構,使用運算符重載,自定義優先級1 7: struct cmp1{ 8: bool operator ()(int &a,int &b){ 9: return a>b;//最小值優先 10: } 11: }; 12: struct cmp2{ 13: bool operator ()(int &a,int &b){ 14: return a<b;//最大值優先 15: } 16: }; 17: //定義結構,使用運算符重載,自定義優先級2 18: struct number1{ 19: int x; 20: bool operator < (const number1 &a) const { 21: return x>a.x;//最小值優先 22: } 23: }; 24: struct number2{ 25: int x; 26: bool operator < (const number2 &a) const { 27: return x<a.x;//最大值優先 28: } 29: }; 30: int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; 31: number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0}; 32: number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0}; 33:? 34: int main() 35: { priority_queue<int>que;//采用默認優先級構造隊列 36:? 37: priority_queue<int,vector<int>,cmp1>que1;//最小值優先 38: priority_queue<int,vector<int>,cmp2>que2;//最大值優先 39:? 40: priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”會被認為錯誤, 41: //這是右移運算符,所以這里用空格號隔開 42: priority_queue<int,vector<int>,less<int> >que4;最大值優先 43:? 44: priority_queue<number1>que5; 45: priority_queue<number2>que6; 46:? 47: int i; 48: for(i=0;a[i];i++){ 49: que.push(a[i]); 50: que1.push(a[i]); 51: que2.push(a[i]); 52: que3.push(a[i]); 53: que4.push(a[i]); 54: } 55: for(i=0;num1[i].x;i++) 56: que5.push(num1[i]); 57: for(i=0;num2[i].x;i++) 58: que6.push(num2[i]); 59:? 60:? 61: printf("采用默認優先關系:/n(priority_queue<int>que;)/n"); 62: printf("Queue 0:/n"); 63: while(!que.empty()){ 64: printf("%3d",que.top()); 65: que.pop(); 66: } 67: puts(""); 68: puts(""); 69:? 70: printf("采用結構體自定義優先級方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n"); 71: printf("Queue 1:/n"); 72: while(!que1.empty()){ 73: printf("%3d",que1.top()); 74: que1.pop(); 75: } 76: puts(""); 77: printf("Queue 2:/n"); 78: while(!que2.empty()){ 79: printf("%3d",que2.top()); 80: que2.pop(); 81: } 82: puts(""); 83: puts(""); 84: printf("采用頭文件/"functional/"內定義優先級:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n"); 85: printf("Queue 3:/n"); 86: while(!que3.empty()){ 87: printf("%3d",que3.top()); 88: que3.pop(); 89: } 90: puts(""); 91: printf("Queue 4:/n"); 92: while(!que4.empty()){ 93: printf("%3d",que4.top()); 94: que4.pop(); 95: } 96: puts(""); 97: puts(""); 98: printf("采用結構體自定義優先級方式二:/n(priority_queue<number>que)/n"); 99: printf("Queue 5:/n"); 100: while(!que5.empty()){ 101: printf("%3d",que5.top()); 102: que5.pop(); 103: } 104: puts(""); 105: printf("Queue 6:/n"); 106: while(!que6.empty()){ 107: printf("%3d",que6.top()); 108: que6.pop(); 109: } 110: puts(""); 111: return 0; 112: } 113: /* 114: 運行結果 : 115: 采用默認優先關系: 116: (priority_queue<int>que;) 117: Queue 0: 118: 91 83 72 56 47 36 22 14 10 7 3 119: 120: 采用結構體自定義優先級方式一: 121: (priority_queue<int,vector<int>,cmp>que;) 122: Queue 1: 123: 3 7 10 14 22 36 47 56 72 83 91 124: Queue 2: 125: 91 83 72 56 47 36 22 14 10 7 3 126: 127: 采用頭文件"functional"內定義優先級: 128: (priority_queue<int,vector<int>,greater<int>/less<int> >que;) 129: Queue 3: 130: 3 7 10 14 22 36 47 56 72 83 91 131: Queue 4: 132: 91 83 72 56 47 36 22 14 10 7 3 133: 134: 采用結構體自定義優先級方式二: 135: (priority_queue<number>que) 136: Queue 5: 137: 3 7 10 14 22 36 47 56 72 83 91 138: Queue 6: 139: 91 83 72 56 47 36 22 14 10 7 3 140: */
轉載于:https://www.cnblogs.com/bovine/archive/2012/03/14/2395901.html
總結
以上是生活随笔為你收集整理的zz STL 优先队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我犯了技术狂热症,应该做减法
- 下一篇: IOS的消息传递机制,使用NSNotif