0
点赞
收藏
分享

微信扫一扫

C++ STL stack最全入门

A邱凌 2022-01-24 阅读 44

堆栈(stack)是一种较简单的常用容器,是一种受限制的向量,只允许在向量的一端存取yua取元素,后进栈的元素先出栈,即LIFO(last in first out)。

STL中的堆栈提供的主要操作如下:

push() //将一个元素加入栈中,加入的元素放在栈顶

top() //返回栈顶元素

pop() //删除栈顶元素

简例:
 

#include<bits/stdc++.h>
using namespace std;

int main()
{
    stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout<<s.top()<<" ";
    s.pop(); s.top()=100;
    s.push(50);
    s.push(60);
    s.pop();
    while(!s.empty())
    {
        cout<<s.top()<<" ";
        s.pop();
    }
    return 0;
}
举报

相关推荐

0 条评论