C++_STL常用容器_stack、queue容器

一、stack 容器

1、stack 容器基本概念

概念:stack 是一种先进后出的数据结构,只有一种接口。

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

2、stack 常用接口

构造函数:

  • stack stk:采用模版类实现,stack对象的默认构造形式

  • stack(const stack &s):拷贝构造函数

赋值操作:

  • stack &operator=(const stack &stk) :重载等号操作符

数据存取:

  • push(elem):添加元素

  • pop():从栈顶移出第一个元素

  • top():返回栈顶元素

大小操作:

  • empty():判断栈是否为空

  • size():返回栈的大小

void te01(){
    int a = 0;
    stack < int > s;
    s.push(10);
    s.push(435);
    s.push(2);
    s.push(62);
    s.push(18);
    s.push(88);
    stack <int> s1;
    s1 = s;

    cout << s.size() << endl;

    while(!empty(s)){
        cout << s.top() << " ";
        s.pop();
    }
    cout << endl;
    while(!empty(s1)){
        cout << s1.top() << " ";
        s1.pop();
    }
    cout << endl;

}

二、queue 容器

1、queue 基本概念

queue 是一种先进先出的数据接口

2、queue 常用接口

构造函数:

  • queue que :queue 对象的默认构造形式

  • queue (const queue &que) :拷贝构造函数

赋值操作:

  • queue &operator = (const queue &que):重载等号操作

数据存取:

  • push(elem):队尾添加元素

  • pop() :对头移出第一个元素

  • back():返回最后一个元素

  • front():返回第一个元素

大小操作:

  • size():返回栈的大小

  • 3:判断栈是否为空

void te02(){
    queue<int> q;
    q.push(10);
    q.push(76);
    q.push(43);
    q.push(72);
    q.push(24);
    q.push(45);

    cout << q.size() << endl;

    cout << q.front() << endl;
    cout << q.back() << endl;
    while(!empty(q)){
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
    cout << q.size() << endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱空nnnn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值