Queue和Stack头文件的使用

一.首先需要说明的是,queue和stack头文件只能用在C++中。

在VC中,不支持#include<queue>和include<stack>

在其他编译器中,如codeblocks,可以选择生成C++文件

头文件的写法有:

C++

#include<iostream>

#include<cstdlib>

#include<queue>

#include<stack>

using namespace std;

或者:

C:  注:<cstdio>是C++为了兼容C而将原来C中的.h文件(如stdio.h)放入std命名空间中,改名问<cstdio>

#include<cstdio>

#include<cstdlib>

#include<queue>

#include<stack>

二.详细说明

1、stack
stack 模板类的定义在<stack>头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。


定义stack 对象的示例代码如下:
stack<int> s1;
stack<string> s2;


stack 的基本操作有:
s.push(x);    入栈
s.pop();        出栈,注意,出栈操作只是删除栈顶元素,并不返回该元素。
s.top();      访问栈顶元素
s.empty(), 判断栈空,当栈空时,返回true。
s.size()        访问栈中元素个数


2、queue
queue 模板类的定义在<queue>头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。


定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
q.push(x);       入队, 将x 接到队列的末端。
q.pop();           出队, 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
q.front(),       访问队首元素,即最早被压入队列的元素。
q.back(),       访问队尾元素,即最后被压入队列的元素。
q.empty(),     判断队空,当队列空时,返回true。
q.size()            访问队中元素个数

看下面这个简单实例:

[cpp]   view plain  copy
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <queue>  
  4. #include <stack>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     int i,j,e,n,m,s;  
  10.     queue<int> q1;  
  11.     stack<int> s1;  
  12.   
  13.     cout<<"Queue test!"<<endl;  
  14.   
  15.     if(q1.empty())  
  16.         cout<<"Queue Empty!"<<endl;  
  17.   
  18.     for(i=0;i<10;i++)  
  19.         q1.push(i);  
  20.   
  21.     if(!(q1.empty()))  
  22.         cout<<"NO Empty"<<endl;  
  23.   
  24.   
  25.     n=q1.size();  
  26.     cout<<"size:"<<n<<endl;  
  27.   
  28.     m=q1.back();  
  29.     cout<<"Tail:"<<m<<endl;  
  30.   
  31.     for(i=0;i<10;i++)  
  32.     {  
  33.         e=q1.front();  
  34.         cout<<"front:"<<e<<endl;  
  35.         q1.pop();  
  36.     }  
  37.   
  38.   
  39.     if(q1.empty())  
  40.         cout<<"Queue empty agagin!"<<endl;  
  41.   
  42.     cout<<endl;  
  43.   
  44.     cout<<"Stack test!"<<endl;  
  45.   
  46.     if(s1.empty())  
  47.         cout<<"stack empty!"<<endl;  
  48.   
  49.     for(i=0;i<10;i++)  
  50.         s1.push(i);  
  51.   
  52.     if(!s1.empty())  
  53.         cout<<"stack not empty!"<<endl;  
  54.   
  55.     n=s1.size();  
  56.     cout<<"size:"<<n<<endl;  
  57.   
  58.   
  59.     for(i=0;i<10;i++)  
  60.     {  
  61.         m=s1.top();  
  62.         cout<<"top:"<<m<<endl;  
  63.         s1.pop();  
  64.     }  
  65.   
  66.     if(s1.empty())  
  67.         cout<<"Stack empty again!"<<endl;  
  68.   
  69.     return 0;  
  70. }  

运行结果:


3、priority_queue
在<queue>头文件中,还定义了另一个非常有用的模板类priority_queue(优先队列)。优先队
列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序
出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
priority_queue 模板类有三个模板参数,第一个是元素类型,第二个容器类型,第三个是比
较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大
的往后排(出队时序列尾的元素出队)。
定义priority_queue 对象的示例代码如下:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // 注意在两个尖括号之间一定要留空格。
priority_queue<int, vector<int>, greater<int> > q3; // 定义小的先出队
priority_queue 的基本操作与queue 相同。
初学者在使用priority_queue 时,最困难的可能就是如何定义比较算子了。
如果是基本数据类型,或已定义了比较运算符的类,可以直接用STL 的less 算子和greater
算子——默认为使用less 算子,即小的往前排,大的先出队。
如果要定义自己的比较算子,方法有多种,这里介绍其中的一种:重载比较运算符。优先队
列试图将两个元素x 和y 代入比较运算符(对less 算子,调用x<y,对greater 算子,调用x>y),
若结果为真,则x 排在y 前面,y 将先于x 出队,反之,则将y 排在x 前面,x 将先出队。
看下面这个简单的示例:

[cpp]   view plain  copy
  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. class T  
  5. {  
  6.     public:  
  7.     int x, y, z;  
  8.     T(int a, int b, int c):x(a), y(b), z(c)  
  9.     {  
  10.     }  
  11. };  
  12. bool operator < (const T &t1, const T &t2)  
  13. {  
  14.     return t1.z < t2.z; // 按照z 的顺序来决定t1 和t2 的顺序  
  15. }  
  16. int main()  
  17. {  
  18.     priority_queue<T> q;  
  19.     q.push(T(4,4,3));  
  20.     q.push(T(2,2,5));  
  21.     q.push(T(1,5,4));  
  22.     q.push(T(3,3,6));  
  23.     while (!q.empty())  
  24.     {  
  25.         T t = q.top(); q.pop();  
  26.         cout << t.x << " " << t.y << " " << t.z << endl;  
  27.     }  
  28.     return 1;  
  29. }  
  30. 输出结果为(注意是按照z 的顺序从大到小出队的):  
  31. 3 3 6  
  32. 2 2 5  
  33. 1 5 4  
  34. 4 4 3  


再看一个按照z 的顺序从小到大出队的例子:

[html]   view plain  copy
  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. class T  
  5. {  
  6.     public:  
  7.     int x, y, z;  
  8.     T(int a, int b, int c):x(a), y(b), z(c)  
  9.     {  
  10.     }     
  11. };  
  12. bool operator > (const T &t1, const T &t2)  
  13. {  
  14.     return t1.z > t2.z;  
  15. }  
  16. int main()  
  17. {  
  18.     priority_queue<T, vector<T>, greater<T> > q;  
  19.     q.push(T(4,4,3));  
  20.     q.push(T(2,2,5));  
  21.     q.push(T(1,5,4));  
  22.     q.push(T(3,3,6));  
  23.     while (!q.empty())  
  24.     {  
  25.         T t = q.top(); q.pop();  
  26.         cout << t.x << " " << t.y << " " << t.z << endl;  
  27.     }  
  28.     return 1;  
  29. }  
  30. 输出结果为:  
  31. 4 4 3  
  32. 1 5 4  
  33. 2 2 5  
  34. 3 3 6  



如果我们把第一个例子中的比较运算符重载为:

[cpp]   view plain  copy
  1. bool operator < (const T &t1, const T &t2)  
  2. {  
  3.     return t1.z > t2.z; // 按照z 的顺序来决定t1 和t2 的顺序  
  4. }  

则第一个例子的程序会得到和第二个例子的程序相同的输出结果。

猜你喜欢

转载自blog.csdn.net/qq_42337888/article/details/80737907