STL stack堆栈容器

来源:互联网 发布:阿利伯克级驱逐舰数据 编辑:程序博客网 时间:2024/04/30 18:01

                                                                                           stack堆栈容器

【stack的定义】

stack的声明:#include<stack>

定义:stack<int>s;


【stack的性质】

           stack堆栈是一个后进先出的线性表,插入和删除都只能在表的一端进行,插入元素叫入栈,删除元素叫      出栈。


【stack的函数】

stack<int>s;

1. s.push(x)                     将元素x入栈

2. s.pop()                         删除栈顶元素

3. s.top()                          读取栈顶元素

4. s.empty()                     判断堆栈是否为空  


【stack的基本操作】

#include<stack>#include<iostream>using namespace std;int main(){    stack<int>s;    // push()将从栈顶元素入栈,stack不能从栈尾入栈    s.push(1);    s.push(2);    s.push(3);    s.push(9);    // top()访问栈顶元素    cout<<s.top()<<endl;    // size()堆栈元素数量    cout<<s.size()<<endl;    // empty()判断堆栈是否为空    cout<<s.empty()<<endl;    while(!s.empty())         <span style="color:#ff0000;">// 经常用到的代码</span>    {        cout<<s.top()<<" ";        // pop()栈顶出栈        s.pop();    }    cout<<endl;    return 0;}


0 0
原创粉丝点击