织梦CMS - 轻松建站从此开始!

罗索

STL 优先队列、队列、栈的使用

jackyhwei 发布于 2011-09-19 19:17 点击:次 
STL中的优先队列、队列、栈的说明及使用示例。
TAG:

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

基本操作:

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

pop() 删除对顶元素

push() 加入一个元素

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

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

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

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

2、自定义优先级:

  1. struct cmp 
  2.     operator bool ()(int x, int y) 
  3.     { 
  4.         return x > y; // x小的优先级高 
  5.       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高 
  6. }; 
  7. priority_queue<int, vector<int>, cmp>q;//定义方法 
  8. //其中,第二个参数为容器类型。第三个参数为比较函数。 

3、结构体声明方式:

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

STL 中队列的使用(queue)

基本操作:

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

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

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

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

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

size() 返回队列的长度

使用方法:

头文件:

#include <queue>

声明方法:

1、普通声明

queue<int>q;

2、结构体

  1. struct node 
  2.     int x, y; 
  3. }; 
  4. 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;//入队元素为自定义型

/**//*
*===================================*
|                                   |
|       STL中优先队列使用方法       |
|                                   |        
|       chenlie                     |
|                                   |
|       2010-3-24                   |
|                                   |
*===================================*
*/

  1. #include <iostream> 
  2. #include <vector> 
  3. #include <queue> 
  4. using namespace std; 
  5. int c[100]; 
  6.  
  7. struct cmp1 
  8.      bool operator ()(int x, int y) 
  9.     { 
  10.         return x > y;//小的优先级高 
  11.     } 
  12. }; 
  13.  
  14. struct cmp2 
  15.     bool operator ()(const int x, const int y) 
  16.     { 
  17.         return c[x] > c[y];  
  18.        // c[x]小的优先级高,由于可以在对外改变队内的值, 
  19.         //所以使用此方法达不到真正的优先。建议用结构体类型。 
  20.     } 
  21. }; 
  22.  
  23. struct node 
  24.     int x, y; 
  25.     friend bool operator < (node a, node b) 
  26.     { 
  27.         return a.x > b.x;//结构体中,x小的优先级高 
  28.     } 
  29. }; 
  30.  
  31.  
  32. priority_queue<int>q1; 
  33.  
  34. priority_queue<int, vector<int>, cmp1>q2; 
  35.  
  36. priority_queue<int, vector<int>, cmp2>q3; 
  37.  
  38. priority_queue<node>q4; 
  39.  
  40.  
  41. queue<int>qq1; 
  42. queue<node>qq2; 
  43.  
  44. int main() 
  45.     int i, j, k, m, n; 
  46.     int x, y; 
  47.     node a; 
  48.     while (cin >> n) 
  49.     { 
  50.         for (i = 0; i < n; i++) 
  51.         { 
  52.             cin >> a.y >> a.x; 
  53.             q4.push(a); 
  54.         } 
  55.         cout << endl; 
  56.         while (!q4.empty()) 
  57.         { 
  58.             cout << q4.top().y << " " << q4.top().x << endl; 
  59.             q4.pop(); 
  60.         } 
  61.     //    cout << endl; 
  62.     } 
  63.     return 0; 

文章:来源

(CodeStream)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201109/15019.html]
本文出处:cppblog.com/CodeStream 作者:CodeStream
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容