STL优先级

  1. <span style="font-size:24px;">struct cmp1  
  2. {    
  3.     bool operator ()(int &a,int &b)  
  4.     {    
  5.         return a>b;//最小值优先     
  6.     }    
  7. };    
  8. struct cmp2  
  9. {    
  10.     bool operator ()(int &a,int &b)  
  11.     {    
  12.         return a<b;//最大值优先     
  13.     }    
  14. };    
  15. struct node1  
  16. {    
  17.     int u;    
  18.     bool operator < (const node1 &a) const   
  19.     {    
  20.        return u>a.u;//最小值优先     
  21.     }    
  22. };    
  23. struct node2  
  24. {    
  25.     int u;    
  26.     bool operator < (const node2 &a) const   
  27.     {    
  28.         return u<a.u;//最大值优先     
  29.     }    
  30. };   
  31. priority_queue<int>q1;//采用默认优先级构造队列       
  32. priority_queue<int,vector<int>,cmp1>q2;//最小值优先     
  33. priority_queue<int,vector<int>,cmp2>q3;//最大值优先     
  34. priority_queue<int,vector<int>,greater<int> >q4;//注意“>>”会被认为错误,     
  35.                                                 //这是右移运算符,所以这里用空格号隔开,最小值优先   
  36. priority_queue<int,vector<int>,less<int> >q5;//最大值优先      
  37. priority_queue<node1>q6;  //自定义优先级  
  38. priority_queue<node2>q7;</span>  

STL 中优先队列的使用方法(priority_queu)

基本操作:

empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int>q;
//通过操作,按照元素从大到小的顺序出队

2、自定义优先级:

  1. struct cmp  
  2. {  
  3.     operator bool ()(int x, int y)  
  4.     {  
  5.         return x > y; // x小的优先级高  
  6.       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高  
  7. }  
  8. };  
  9. priority_queue<int, vector<int>, cmp>q;//定义方法  

  1. //其中,第二个参数为容器类型。第三个参数为比较函数。  

3、结构体声明方式:

  1. struct node  
  2. {  
  3.     int x, y;  
  4.     friend bool operator < (node a, node b)  
  5.     {  
  6.         return a.x > b.x; //结构体中,x小的优先级高  
  7.     }  
  8. };  
  9. <span style="font-size:24px;">priority_queue<node>q;//定义方法</span>  
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误

STL 中队列的使用(queue)

基本操作:

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

 

使用方法:

头文件:

#include <queue>

 声明方法:

1、普通声明

queue<int>q;


2、结构体

struct node
{
    int x, y;
};
queue<node>q;

STL 中栈的使用方法(stack)

基本操作:

push(x) 将x加入栈中,即入栈操作

pop() 出栈操作(删除栈顶),只是出栈,没有返回值

top() 返回第一个元素(栈顶元素)

size() 返回栈中的元素个数

empty() 当栈为空时,返回 true


使用方法:

和队列差不多,其中头文件为:

#include <stack>


定义方法为:

stack<int>s1;//入栈元素为 int 型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
  1. /**//* 
  2. *===================================* 
  3. |                                   | 
  4. |       STL中优先队列使用方法       | 
  5. |                                   |         
  6. |       chenlie                     | 
  7. |                                   | 
  8. |       2010-3-24                   | 
  9. |                                   | 
  10. *===================================* 
  11. */  
  12. #include <iostream>  
  13. #include <vector>  
  14. #include <queue>  
  15. using namespace std;  
  16. int c[100];  
  17.   
  18. struct cmp1  
  19. {  
  20.      bool operator ()(int x, int y)  
  21.     {  
  22.         return x > y;//小的优先级高  
  23.     }  
  24. };  
  25.   
  26. struct cmp2  
  27. {  
  28.     bool operator ()(const int x, const int y)  
  29.     {  
  30.         return c[x] > c[y];   
  31.        // c[x]小的优先级高,由于可以在对外改变队内的值,  
  32.         //所以使用此方法达不到真正的优先。建议用结构体类型。  
  33.     }  
  34. };  
  35.   
  36. struct node  
  37. {  
  38.     int x, y;  
  39.     friend bool operator < (node a, node b)  
  40.     {  
  41.         return a.x > b.x;//结构体中,x小的优先级高  
  42.     }  
  43. };  
  44.   
  45.   
  46. priority_queue<int>q1;  
  47.   
  48. priority_queue<int, vector<int>, cmp1>q2;  
  49.   
  50. priority_queue<int, vector<int>, cmp2>q3;  
  51.   
  52. priority_queue<node>q4;  
  53.   
  54.   
  55. queue<int>qq1;  
  56. queue<node>qq2;  
  57.   
  58. int main()  
  59. {  
  60.     int i, j, k, m, n;  
  61.     int x, y;  
  62.     node a;  
  63.     while (cin >> n)  
  64.     {  
  65.         for (i = 0; i < n; i++)  
  66.         {  
  67.             cin >> a.y >> a.x;  
  68.             q4.push(a);  
  69.         }  
  70.         cout << endl;  
  71.         while (!q4.empty())  
  72.         {  
  73.             cout << q4.top().y << " " << q4.top().x << endl;  
  74.             q4.pop();  
  75.         }  
  76.     //    cout << endl;  
  77.     }  
  78.     return 0;  
  79. }  
  1. #include<stdio.h>  
  2. #include<queue>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. struct comp  
  6. {  
  7.     bool operator()(int &x,int &y)  
  8.     {  
  9.         return x<y;  
  10.     }  
  11. };  
  12. priority_queue<int,vector<int>,comp>q;/*优先队列*/  
  13. int main()  
  14. {  
  15.     q.push(2);  
  16.     q.push(1);  
  17.     q.push(3);  
  18.     while(!q.empty())  
  19.     {  
  20.         printf("%d ",q.top());  
  21.         q.pop();  
  22.     }  
  23.     printf("\n");  
  24.     return 0;  
  25. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值