目录

1 stack基本概念

2 stack常用接口


1 stack基本概念

概念:stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口

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

栈中进入数据称为——入栈 push

栈中弹出数据称为——出栈 pop

生活中的栈:弹夹

2 stack常用接口

功能描述:栈容器常用的对外接口

构造函数:

○stack<T> stk; //stack采用模板类实现,stack对象的默认构造形式

○stack(const stack &stk); //拷贝构造函数

赋值操作:

○stack& operator=(const stack &stk); //重载等号操作符

数据存取:

○push(ele); //向栈顶添加元素

○pop(); //从栈顶移除第一个元素

○top(); //返回栈顶元素

大小操作:

○empty(); //判断堆栈是否为空

○size(); //返回栈的大小

#include<iostream>
using namespace std;
#include<stack>

//栈stack容器
void test01()
{
    //特点:符合先进后出数据结构
    stack<int> s;

    //入栈
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    
    cout << "the size of stack is :" << s.size() << endl;
    //只要栈不为空,查看栈顶,并且执行出栈操作
    while (!s.empty())
    {
        //查看栈顶元素
        cout << "stack top's element is : " << s.top() << endl;

        //出栈
        s.pop();
    }

    cout << "the size of stack is :" << s.size() << endl;
}

int main(){
    test01();
    system("pause");
    return 0;
} 

总结:

○入栈——push

○出栈——pop

○返回栈顶——top

○判断栈是否为空——empty

○返回栈大小——size

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐