堆栈的头函数使用

1.若要使用 栈,需要有头函数#include<stack>

 

2.STL容器stack成员函数

 

        bool empty()  判断是否为空

 

        void pop()  删除栈顶元素

 

        void push(const TYPE &val)  进栈

 

        TYPE &top()  查看栈顶元素

 

        size_type size()  返回栈中

#include <iostream>
#include <stack>
using namespace std;
 
int main() {
    stack<int> s;
    s.push(1);
    s.push(2);
    s.push(3);
    cout<<s.top()<<endl;
    cout<<s.size()<<endl;
    s.pop();
    cout<<s.size()<<endl;
    if(s.empty())
        cout<<"Empty"<<endl;
    else
        cout<<"Not Empty"<<endl;
    return 0;
}

 

元素数目

 

3.实例

 

posted @ 2018-11-07 12:25  Stephen~Jixing  阅读(379)  评论(0编辑  收藏  举报