zoukankan      html  css  js  c++  java
  • STL之适配器

    一,STL适配器简介

    1.什么是适配器

      STL提供了序列式容器,同时针对序列式容器提供了应用于不同场景的容器适配器,通俗讲适配器就是以序列式容器为底层数据结构,进一步封装了的为适应场景应用的容器。STL中提供了三种适配器,分别为stack,queue和priority_queue。

    二,堆栈(stack)

    1.堆栈的基本概念

    • stack是一种“先进后出”的容器。
    • stack的默认底层数据结构是使用的deque。因此其本质是简单装饰了deque容器而形成的一种新容器。
    • 使用堆栈容器,首先要引入头文件# include<stack>。

    2.stack的构造函数

    // stack的默认构造函数
    stack<char> s1;
    // 压栈
    s1.push('A');
    s1.push('B');
    s1.push('C');
    // stack的拷贝构造函数
    stack<char> s2 = s1;

    3.stack的操作符重载

    // stack的默认构造函数
    stack<char> s1;
    // 压栈
    s1.push('A');
    s1.push('B');
    s1.push('C');
    // stack的拷贝构造函数
    stack<char> s2;
    // 赋值操作符重载
    s2 = s1;

    4.stack的成员函数

    // stack的默认构造函数
    stack<char> s1;
    // 压栈
    s1.push('A');
    s1.push('B');
    s1.push('C');
    // 判断栈是否为空
    while (!s1.empty())
    {
        // 获取栈顶元素
        char top = s1.top();
        // 输出栈顶元素
        cout << "top = " << top << endl;
        // 弹栈:将栈顶元素弹出
        s1.pop();
    }
    // 获取栈的长度
    int size = s1.size();
    cout << "size = " << size << endl;

    三,队列(queue)

    1.队列的基本概念

    • 队列(queue)是一种“先进先出”的容器。
    • 队列(queue)是简单的装饰了“deque”容器而形成的一种新的容器。
    • 使用队列,要先引入头文件# include<queue>。

    2.queue的构造函数

    // 无参构造函数
    queue<int> q1;
    // 队尾添加元素
    q1.push(1);
    q1.push(2);
    q1.push(3);
    // 拷贝构造函数
    queue<int> q2 = q1;

    3.queue的操作符重载

    // 无参构造函数
    queue<int> q1;
    // 队尾添加元素
    q1.push(1);
    q1.push(2);
    q1.push(3);
    // 无参构造函数
    queue<int> q2;
    // 赋值操作符重载
    q2 = q1;

    4.queue的成员函数

    // 无参构造函数
    queue<int> q1;
    // 队尾添加元素
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.push(4);
    q1.push(5);
    q1.push(6);
    // 判断队列是否为空
    while (!q1.empty())
    {
        // 获取队列头部元素
        int first = q1.front();
        // 获取队列尾部元素
        int last = q1.back();
        // 输出头部和尾部元素
        cout << "first = " << first << ",last = " << last << endl;
        // 移除头部元素
        q1.pop();
    }
    // 获取队列的长度
    int size = q1.size();
    cout << "size = " << size << endl;

    四,优先级队列(priority_queue)

    1.优先级队列的基本知识

    • 优先级队列是一种特殊的队列,它能够在队列中进行排序,默认的优先级队列是最大值优先级队列,即最大的元素在队列的头部。
    • 优先级队列底层实现结构是vector,还可以使用deque,但是不能使用list。
    • 优先级队列使用的# include<queue>头文件和对列是一个头文件。
    • 优先级队列使用的是堆排序。
    • 除了与在构造队列时的不同,其他的操作都相同。

    2.优先级队列代码示例

    // 默认是最大值优先级队列
    priority_queue<int> pq1;
    // 这是最大值优先级队列的另一种写法
    priority_queue<int, vector<int>, less<int>> pq2;
    // 这是最小值优先级队列
    priority_queue<int, vector<int>, greater<int>> pq3;
    
    // 往最大值优先级队列添加元素
    pq2.push(2);
    pq2.push(1);
    pq2.push(3);
    pq2.push(0);
    // 判断是否为空
    while (!pq2.empty())
    {
        // 获取头部元素
        int top = pq2.top();
        // 输出
        cout << "top = " << top << endl;
        // 弹出头部元素
        pq2.pop();
    }
    // 输出结果为:3,2,1,0
  • 相关阅读:
    新能源汽车产业发展规划(2021-2035年)
    Spire.Doc 生成长图
    【Kafka】单机Kafka测试
    【Linux】IO多路复用之select、poll、epoll模型
    【Linux】五种IO模型
    【Linux】零拷贝技术(Zero Copy)
    【Linux】系统相关概念
    【SpringBoot】SpringBoot 整合Redisson
    【Spring】SpringBoot + SpringSession + Redis 实现Session共享
    【SpringBoot】SpringBoot 整合Redis
  • 原文地址:https://www.cnblogs.com/metalsteel/p/6298123.html
Copyright © 2011-2022 走看看