头文件: #include<stack>
实例化: stack<类型[, 存储容器]>StackName
成员函数:
bool empty(); |
栈为空返回 true 否则返回false. |
void pop(); |
移除堆栈中最顶层元素。 |
void push( const TYPE &val ); |
将 val 值压栈,使其成为栈顶的第一个元素 |
size_type size(); |
返当前堆栈中的元素数目 |
TYPE &top(); |
返回对栈顶元素的引用 |
实例程序:
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
void main()
{
stack<int>s;
if (s.empty() == true) cout << "栈为空返回true";
s.push(1); //将val值压栈,成为栈顶的第一个元素
s.push(2);
s.push(3);
cout << "当前栈中元素数目:"<< s.size() << endl; //3
cout << "返回对栈顶元素的引用:" << s.top() << endl; //3
s.pop(); //移除栈最顶层的元素数组
cout << "当前栈中元素数目:" << s.size() << endl; //2
cout << "返回对栈顶元素的引用:" << s.top() << endl; //2
system("pause");
}