STL中stack的使用

目录

一、stack类介绍和使用

stack类介绍

stack类定义

stack类常见构造函数

stack数据操作

empty()函数

top() pop() 和 push() 函数 

size()函数

swap()函数


一、stack类介绍和使用

stack类介绍

1.stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。

2.stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定的成员函数来访问其元素,将特定类作为其底层的,元素特定容器的尾部(即栈顶)被压入和弹出

3.stack的底层容器可以是任何标准的容器类模版或者一些其他特定的容器类,这些容器类应该支持一下操作:

·empty:判空操作

·back:获取尾部元素操作

·push_back:尾部插入元素操作

·pop_back:尾部删除元素操作

4.标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stack指定特定的底层容器,默认情况下使用deque

stack类定义

template <class T, class Container = deque<T> > class stack;

stack类为类模板,所以在使用时需要带上类型表示一个具体的类,例如数据类型为int类型的stack使用时需要写为stack<int>

stack类常见构造函数

#include <iostream>
using namespace std;
#include <stack>
int main(){
    stack<int> st;
    st.push(1);
    st.push(3);
    st.push(1);
    st.push(4);
    
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;
    
    return 0;
}
//4 1 3 1 

stack数据操作

empty()函数

使用empty()函数可以判断调用对象栈是否为空栈

#include <iostream>
using namespace std;
#include <stack>
int main(){
    stack<int> st;
    st.push(1);
    st.push(3);
    st.push(1);
    st.push(4);
    
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;
    
    cout << "栈是否为空:";
    cout << st.empty() << endl;
    return 0;
}
输出结果:
4 1 3 1 
栈是否为空:1

top() pop() 和 push() 函数 

top()函数获取调用对象栈中的栈顶元素

pop()函数可以弹出调用对象栈的栈顶元素

 

push()函数可以向调用对象栈内插入数据

注意:

1️⃣当栈中没有元素时,调用pop()函数会出现断言错误

2️⃣如果栈为空时取栈内元素将会出现断言错误

#include <iostream>
using namespace std;
#include <stack>
int main(){
    stack<int> st;
    st.push(4);
    st.push(1);
    st.push(3);
    st.push(1);
    
    while (!st.empty()) {
        cout << st.top() << " ";//取出栈顶元素
        st.pop();//弹出栈顶元素
    }
    cout << endl;

    return 0;
}

size()函数

size()函数可以获取调用对象栈中的有效数据个数

 

#include <iostream>
using namespace std;
#include <stack>
int main(){
    stack<int> st;
    st.push(4);
    st.push(1);
    st.push(3);
    st.push(1);
    
    cout << "栈中数据个数为:" <<st.size() << endl;
    
    while (!st.empty()) {
        cout << st.top() << " ";//取出栈顶元素
        st.pop();//弹出栈顶元素
    }
    cout << endl;
    
    cout << "栈中数据个数为:" <<st.size() << endl;

    return 0;
}
输出结果:
栈中数据个数为:4
1 3 1 4 
栈中数据个数为:0

swap()函数

swap()函数可以交换调用对象栈和指定对象栈

#include <iostream>
using namespace std;
#include <stack>
int main(){
    stack<int> st;
    st.push(4);
    st.push(1);
    st.push(3);
    st.push(1);
    
    stack<int> st1;
    st1.push(0);
    st1.push(2);
    st1.push(5);
    
    st.swap(st1);
    while (!st1.empty()) {
        cout << st1.top() << " ";//取出栈顶元素
        st1.pop();//弹出栈顶元素
    }
    cout << endl;

    
    while (!st.empty()) {
        cout << st.top() << " ";//取出栈顶元素
        st.pop();//弹出栈顶元素
    }
    cout << endl;
    return 0;
}
输出结果:
1 3 1 4 
5 2 0 
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值