stl 中的栈的学习
TL 中栈的使用方法(stack)
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
使用方法和队列基本一样。
头文件
#include<stack>
定义方法:
stack<int>s1;//入栈元素为 int 型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
#include<stack> #include<iostream> using namespace std; int main( ) { int i,j; stack<int>q; stack<string>p; for(i=0;i<5;i++) q.push(i); p.push("china"); cout<<p.top()<<"\n"; cout<<q.size()<<"\n"; while(!q.empty()) { cout<<q.top(); q.pop(); } system("pause"); return 0; }
posted on 2011-05-07 17:21 more think, more gains 阅读(271) 评论(0) 编辑 收藏 举报