栈是一种线性表,只可以在一端进行数据插入、弹出,C++的STL提供栈类,需要包含头文件<stack>
,有以下成员函数:
成员函数 | 功能 |
---|---|
empty() | test whther the container is empty |
size() | returns the size of the container |
push() | insert element into the container |
pop() | remove the top element |
top() | returns the top element |
swap() | swap elements of two stacks |
#include <iostream>
#include <stack>
int main(){
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.size() << endl;
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
}