C++中的STL——stack类的基本使用

目录

stack类介绍

stack类定义

stack类常见构造函数

stack数据操作

empty()函数

size()函数

top()函数

push()函数

pop()函数

swap()函数


stack类介绍

  1. stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。
  2. stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定的成员函数来访问其元素,将特定类作为其底层的,元素特定容器的尾部(即栈顶)被压入和弹出。
  3. stack的底层容器可以是任何标准的容器类模板或者一些其他特定的容器类
  4. 标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stack指定特定的底层容器,默认情况下使用deque。

stack类定义

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

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

stack类常见构造函数

构造函数

函数原型

无参构造函数

explicit stack (const container_type& ctnr = container_type());

📌

上面表格中的前三个构造函数均含有自定义空间配置器并带有缺省值,目前只使用默认即可

📌

使用stack类需要包含头文件<stack>

示例代码:

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

int main()
{
    stack<int> st;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);

    //打印栈
    while (!st.empty())
    {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    return 0;
}
输出结果:
5 4 3 2 1

stack数据操作

函数

功能

empty()

判断调用对象栈是否为空栈

size()

获取调用对象栈中的有效数据个数

top()

获取调用对象栈中的栈顶元素

push()

向调用对象栈顶插入元素

pop()

弹出调用对象栈顶元素

swap()

交换调用对象栈和指定栈

empty()函数

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

函数

函数原型

empty()

bool empty() const;

示例代码:

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

int main()
{
    stack<int> st;
    stack<int> st1;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);

    cout << "st: " << st.empty() << endl;
    cout << "st1: " << st1.empty() << endl;
    return 0;
}
输出结果:
st: 0
st1: 1

size()函数

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

函数

函数原型

size()

size_type size() const;

示例代码:

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

int main()
{
    stack<int> st;
    stack<int> st1;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);

    cout << "st: " << st.size() << endl;
    cout << "st1: " << st1.size() << endl;
    return 0;
}
输出结果:
st: 5
st1: 0

top()函数

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

函数

函数原型

top()

value_type& top();

const value_type& top() const;

📌

注意,如果栈为空时取栈内元素将会出现断言错误

示例代码:

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

int main()
{
    stack<int> st;
    stack<int> st1;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);

    cout << "st: " << st.top() << endl;
    // 断言错误
    //cout << "st1: " << st1.top() << endl;
    return 0;
}
输出结果:
5

push()函数

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

函数

函数原型

push()

void push (const value_type& val);

示例代码:

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

int main()
{
    stack<int> st;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);

    //打印栈
    while (!st.empty())
    {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    return 0;
}
输出结果:
5 4 3 2 1

pop()函数

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

函数

函数原型

pop()

void pop();

📌

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

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

int main()
{
    stack<int> st;
    stack<int> st1;
    st.push(1);
    st.push(2);
    st.push(3);
    cout << st.top() << " ";
    st.pop();
    st.push(4);
    st.push(5);
    //打印栈
    while (!st.empty())
    {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    // 断言错误
    //cout << "st1: " << st1.top() << endl;
    return 0;
}
输出结果:
3 5 4 2 1

swap()函数

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

函数

函数原型

swap()

void swap (stack& x);

示例代码:

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

int main()
{
    stack<int> st;
    stack<int> st1;

    st.push(1);
    st.push(1);
    st.push(1);
    st.push(1);
    st.push(1);

    st1.push(2);
    st1.push(2);
    st1.push(2);
    st1.push(2);
    st1.push(2);

    cout << "交换前:" << endl;
    //打印栈
    while (!st.empty())
    {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    //打印栈
    while (!st1.empty())
    {
        cout << st1.top() << " ";
        st1.pop();
    }
    cout << endl;

    // 注意打印已经使栈为空,需要重新插入元素
    st.push(1);
    st.push(1);
    st.push(1);
    st.push(1);
    st.push(1);

    st1.push(2);
    st1.push(2);
    st1.push(2);
    st1.push(2);
    st1.push(2);

    st.swap(st1);

    cout << "交换后:" << endl;

    //打印栈
    while (!st.empty())
    {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    //打印栈
    while (!st1.empty())
    {
        cout << st1.top() << " ";
        st1.pop();
    }
    cout << endl;

    return 0;
}
交换前:
1 1 1 1 1
2 2 2 2 2
交换后:
2 2 2 2 2
1 1 1 1 1

相关推荐

  1. C++STL——stack基本使用

    2024-04-26 23:04:02       16 阅读
  2. C++STL——queue基本使用

    2024-04-26 23:04:02       10 阅读
  3. C++抽象

    2024-04-26 23:04:02       16 阅读
  4. C调用C++

    2024-04-26 23:04:02       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-26 23:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-26 23:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-26 23:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-26 23:04:02       20 阅读

热门阅读

  1. web前端第三次笔记

    2024-04-26 23:04:02       16 阅读
  2. centos常用命令(持续更新)

    2024-04-26 23:04:02       19 阅读
  3. CSS - 定位详解

    2024-04-26 23:04:02       12 阅读
  4. 在 TypeScript 中declare module 关键字用法

    2024-04-26 23:04:02       13 阅读
  5. Git命令(附:CMD常用指令)

    2024-04-26 23:04:02       18 阅读
  6. 一步一步写线程之十memory_order的应用

    2024-04-26 23:04:02       15 阅读
  7. ubuntu 下 vim 的使用

    2024-04-26 23:04:02       23 阅读
  8. DRF学习之全局异常处理、接口文档书写

    2024-04-26 23:04:02       17 阅读
  9. vue2指令

    2024-04-26 23:04:02       13 阅读
  10. 新增表单按钮添加点击事件

    2024-04-26 23:04:02       13 阅读