C++ STL 之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::empty

bool empty ( ) const;

判断是否为空。

Return Value

true if the container size is 0false otherwise.

[cpp]  view plain copy
  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 ( );

在栈的顶部移除元素。

 


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

在栈顶添加元素


 
 
  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;

计算栈对象元素个数

 


     
     
  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: 0
1. size: 5
2. size: 4

stack::top

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

返回栈顶元素

[cpp]  view plain copy
  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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值