STL - stack容器

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

基本概念

stack是一种先进后出first in Last out,FILO的数据结构他只有一个出口

栈中只有顶端的元素才可以被外界使用因此栈不允许有遍历操作

     

stack的常用接口

 

#include <algorithm> //算法
#include <iostream>
#include <stack> //栈
#include <string>
using namespace std;
//栈
//不允许遍历遍历是指在元素不发生改变的情况下过去数据

void test01()
{
    stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);

    //只要栈不为空就查看栈顶并弹出
    while (!s.empty()) {
        //查看栈顶元素
        cout << "栈顶元素" << s.top() << endl;
        //出站
        s.pop();
    }
    cout << "栈中元素大小" << s.size() << endl;
}
int main()
{
    test01();
}

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6