[c++] <stack>

特性

  • stack<T>:封装了deque<T>容器的适配器类模板
  • 实现了后入先出
  • 实现编辑器中的undo(撤销)机制

函数

  • push():将一个元素放入栈内
  • top():返回stack内的下一个元素
  • pop():从stack中移除元素
  • size():返回成员个数
  • empty():返回是否为空

示例

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 
 5 int main(){
 6     stack<int> s;
 7     for(int i = 0 ; i < 10 ; i ++ )
 8         s.push(i);
 9     cout<<"栈s中的元素个数为:"<<s.size()<<endl;
10     
11     while(!s.empty()){
12         cout<<" "<<s.top();
13         s.pop();
14     }
15     cout<<endl;
16     
17     if(s.empty()){
18         cout<<"栈s为空"<<endl;
19     }
20     
21     return 0;
22 }
View Code

参考

C++中stack、queue、vector的用法详解

https://www.jb51.net/article/122462.htm

猜你喜欢

转载自www.cnblogs.com/cxc1357/p/12269922.html