STL中stack与queue库函数 的使用方法

来源:互联网 发布:科瑞明软件 怎么样 编辑:程序博客网 时间:2024/05/21 11:22

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()。

 

成员函数

stack::stack

explicit stack ( const Container& ctnr = Container() );

用于构造一个栈适配器对象。

ctnr
Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).
[cpp] view plaincopy
  1. // test_stack.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <stack>  
  6. #include <vector>  
  7. #include <deque>  
  8. #include <iostream>  
  9.   
  10. using namespace std;  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     deque<int> mydeque(2,100);  
  15.     vector<int> myvector(2,200);  
  16.   
  17.     stack<int> first;  
  18.     stack<int> second(mydeque);  
  19.   
  20.     stack<int,vector<int> > third;  
  21.     stack<int,vector<int> > fourth(myvector);  
  22.   
  23.     cout << "size of first: " << (int) first.size() << endl;  
  24.     cout << "size of second: " << (int) second.size() << endl;  
  25.     cout << "size of third: " << (int) third.size() << endl;  
  26.     cout << "size of fourth: " << (int) fourth.size() << endl;  
  27.   
  28.   
  29.     return 0;  
  30. }  

output:

size of first: 0size of second: 3size of third: 0size of fourth: 2

stack::empty

bool empty ( ) const;

判断是否为空。

Return Value

true if the container size is 0false otherwise.

[cpp] view plaincopy
  1. // stack::empty  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   int sum (0);  
  10.   
  11.   for (int i=1;i<=10;i++) mystack.push(i);  
  12.   
  13.   while (!mystack.empty())  
  14.   {  
  15.      sum += mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   
  19.   cout << "total: " << sum << endl;  
  20.     
  21.   return 0;  
  22. }  

Output:

total: 55

stack::pop

void pop ( );

在栈的顶部移除元素。

 

[cpp] view plaincopy
  1. // stack::push/pop  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   
  10.   for (int i=0; i<5; ++i) mystack.push(i);  
  11.   
  12.   cout << "Popping out elements...";  
  13.   while (!mystack.empty())  
  14.   {  
  15.      cout << " " << mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   cout << endl;  
  19.   
  20.   return 0;  
  21. }  

 

Output:

Popping out elements... 4 3 2 1 0

 

stack::push

void push ( const T& x );

在栈顶添加元素

[cpp] view plaincopy
  1. // stack::push/pop  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   
  10.   for (int i=0; i<5; ++i) mystack.push(i);  
  11.   
  12.   cout << "Popping out elements...";  
  13.   while (!mystack.empty())  
  14.   {  
  15.      cout << " " << mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   cout << endl;  
  19.   
  20.   return 0;  
  21. }  

Output:

Popping out elements... 4 3 2 1 0

stack::size

 
size_type size ( ) const;

计算栈对象元素个数

 

[cpp] view plaincopy
  1. // stack::size  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> myints;  
  9.   cout << "0. size: " << (int) myints.size() << endl;  
  10.   
  11.   for (int i=0; i<5; i++) myints.push(i);  
  12.   cout << "1. size: " << (int) myints.size() << endl;  
  13.   
  14.   myints.pop();  
  15.   cout << "2. size: " << (int) myints.size() << endl;  
  16.   
  17.   return 0;  
  18. }  



Output:

0. size: 01. size: 52. size: 4

stack::top

 
      value_type& top ( );const value_type& top ( ) const;

返回栈顶元素

[cpp] view plaincopy
  1. // test_stack.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <stack>  
  6. #include <vector>  
  7. #include <deque>  
  8. #include <iostream>  
  9.   
  10. using namespace std;  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     stack<int> mystack;  
  15.     mystack.push(10);  
  16.     mystack.push(20);  
  17.     mystack.top()-=5;  
  18.     cout << "mystack.top() is now " << mystack.top() << endl;  
  19.   
  20.     return 0;  
  21. }  

Output:

mystack.top() is now 15

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()

#include <cstdlib>
#include <iostream>
#include <queue>

using namespace std;

int main()
{
    int e,n,m;
    queue<int> q1;
    for(int i=0;i<10;i++)
       q1.push(i);
    if(!q1.empty())
    cout<<"dui lie  bu kong\n";
    n=q1.size();
    cout<<n<<endl;
    m=q1.back();
    cout<<m<<endl;
    for(int j=0;j<n;j++)
    {
       e=q1.front();
       cout<<e<<" ";
       q1.pop();
    }
    cout<<endl;
    if(q1.empty())
    cout<<"dui lie  bu kong\n";
    system("PAUSE");
    return 0;
}

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 将先出队。
看下面这个简单的示例:
#include <iostream>

#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator < (const T &t1, const T &t2)
{
return t1.z < t2.z; // 按照z 的顺序来决定t1 和t2 的顺序
}
main()
{
priority_queue<T> q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}
输出结果为(注意是按照z 的顺序从大到小出队的):
3 3 6
2 2 5
1 5 4
4 4 3
再看一个按照z 的顺序从小到大出队的例子:
#include <iostream>
#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator > (const T &t1, const T &t2)
{
return t1.z > t2.z;
}
main()
{
priority_queue<T, vector<T>, greater<T> > q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}
输出结果为:
4 4 3
1 5 4
2 2 5
3 3 6
如果我们把第一个例子中的比较运算符重载为:
bool operator < (const T &t1, const T &t2)
{
return t1.z > t2.z; // 按照z 的顺序来决定t1 和t2 的顺序
}
则第一个例子的程序会得到和第二个例子的程序相同的输出结果。

0 0
原创粉丝点击