文章源自:http://blog.csdn.net/cmm0401/article/details/71978187?locationNum=9&fps=1

C++ STL标准库——栈stack介绍

C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。

C++ STL栈stack的头文件:   #include <stack> 

C++ STL栈stack的成员函数介绍:

操作比较和分配堆栈

empty()堆栈为空,则返回true。

pop()移除栈顶元素。

push()在栈顶增加元素。

size()返回栈中元素数目。

top()返回栈顶元素。


C++ STL栈stack用法代码举例1

[cpp]  view plain  copy
  1. #include "stdafx.h"    
  2. #include <stack>    
  3. #include <vector>    
  4. #include <deque>    
  5. #include <iostream>    
  6.      
  7. using namespace std;    
  8.      
  9. int _tmain(int argc, _TCHAR* argv[])    
  10. {    
  11.     deque<int> mydeque(2,100);    
  12.     vector<int> myvector(2,200);    
  13.      
  14.     stack<int> first;    
  15.     stack<int> second(mydeque);    
  16.      
  17.     stack<int,vector<int> > third;    
  18.     stack<int,vector<int> > fourth(myvector);    
  19.      
  20.     cout << "size of first: " << (int) first.size() << endl;    
  21.     cout << "size of second: " << (int) second.size() << endl;    
  22.     cout << "size of third: " << (int) third.size() << endl;    
  23.     cout << "size of fourth: " << (int) fourth.size() << endl;    
  24.   
  25.     return 0;    
  26. }  

c++ stl栈stack用法代码举例2

[cpp]  view plain  copy
  1. <strong><span style="color:#FF0000;">// stack::empty  </span></strong>  
  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. }  

  c++ stl栈stack用法代码举例3

[cpp]  view plain  copy
  1. <span style="color:#FF0000;"><strong>// stack::push/pop  </strong></span>  
  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. }  
 c++ stl栈stack用法代码举例4

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



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐