zoukankan      html  css  js  c++  java
  • C++容器适配器stack 重载默认容器类型

    容器适配器stack 重载默认容器类型

    1、何为适配器?
    标准库中的顺序容器有以下六种:
    vector、deque、list、forward_list、array,string。
    标准库定义了三种顺序容器适配器:stack、queue和 priority_queue。
    适配器是标准库中的一个通用的概念。容器、迭代器和函数都有适配器。
    本质上,适配器是一种机制,能使某种事物的行为看起来像另一种事物一样。
    一种容器适配器接受一种已有的容器类型,使其行为看起来像一种不同的类型。

    2、定义一个适配器
    每个适配器都定义两个构造函数:默认构造函数创建一个空对象,接收一个容器的构造函数拷贝该容器来初始化适配器。
    例如,假定deq是一个deque<>,就可以用deq来初始化一个新的stack

    stack<int> s(deq);//从deq拷贝元素到s中,其中deque<int> deq;

    适配器s: 使队列deq的行为看起来像栈s一样。、

    3、适配器的默认容器类型
    默认情况下,
    stack 和 queue 是基于 deque 实现的
    priority_queue 是基于 vector 实现的
    同时,
    stack 和 queue 也可以在list或vector上实现,本文就是针对 stack适配器在vector上的实现展开的。
    priority_queue 也可以在 vector上实现。

    4、如何重载默认容器类型?
    创建一个适配器时将一个命名的顺序容器作为第二个类型参数,来重载默认容器类型。
    eg:

    //用vector 重载stack的默认容器 queue ,并且对其进行了初始化
    stack<int, vector<int>> s(v); 

    具体实现:

        vector<int> v;
        for (int i = 0; i < 10; ++i)
            v.push_back(10 * i);
    
        //创建一个适配器时讲一个命名的顺序容器作为第二个类型参数,来重载默认容器类型
        stack<int, vector<int>> s(v); //在vector上实现空栈
        while (!s.empty())
        {
            cout << setw(4) << s.top();
            s.pop();
        }
        cout << endl;

    注意区分下列四种 stack适配器 的实现:

    stack<int, vector<int>> s(v); //是基于 vector<int>实现的,基础元素为 int,
                                  //用 vector<int> 来初始化 s
    stack<int> stk(deq);          //是基于 queue<int>实现的,基础元素为 int,
                                  //用 queue<int> 来初始化  stk
    stack<vector<int>> s2(d);     //是基于 queue<vector<int>>实现的,基础元素为 vector<int>, 
                                  //用 queue<vector<int>> 来初始化 s2
    stack<vector<int>> s1;        //是基于 queue<vector<int>>实现的,基础元素为 vector<int>, 
                                  //未用 queue<vector<int>> 来初始化 s1

    5、测试代码

    //重载默认容器
    #include<iostream>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<iomanip>//包含setw()
    using namespace std;
    
    void dispStack(stack<int> s)
    {
        while (!s.empty())
        {
            cout << setw(4) << s.top();
            s.pop();
        }
        cout << endl;
    }
    
    void diapVector(vector<int> v)
    {
        for (auto c : v)
            cout << setw(4) << c;
        cout << endl;
    }
    
    int main()
    {
        //---------------------基于deque实现stack---------------------------------
        deque<int> deq;
        for (int i = 0; i < 10; ++i)
            deq.push_back(i);
        for (auto c : deq)
            cout << setw(4) << c;
        cout << endl;
    
        stack<int> stk(deq);//用deq来初始化一个新的栈
        /*stack<int> stk; //不能分开进行 初始化
        stk(deq);*/
        dispStack(stk);
        //-------------------------------------------------------------------------
    
        //---------------------基于vector实现stack---------------------------------
        vector<int> v;
        for (int i = 0; i < 10; ++i)
            v.push_back(10 * i);
    
        //stack<int> s(v);//这样不行,stack默认的是基于deque构造  
        //要想基于vector或list,必须用这样的方式:stack<int, vector<int>> s(v);
    
        //创建一个适配器时讲一个命名的顺序容器作为第二个类型参数,来重载默认容器类型
        stack<int, vector<int>> s(v); //在vector上实现空栈
        while (!s.empty())
        {
            cout << setw(4) << s.top();
            s.pop();
        }
        cout << endl;
        //----------------------stack<vector<int>> s1-----------------------------
        vector<int> v2,v3;
        for (int i = 0; i < 10; ++i)
        {
            v2.push_back(20 * i);
            v3.push_back(30 * i);
        }
        stack<vector<int>> s1; //区分 stack<int, vector<int>> s(v); 与 stack<vector<int>> s1;
        s1.push(v);
        s1.push(v2);
        s1.push(v3);
        while (!s1.empty())
        {
            diapVector(s1.top());
            s1.pop();
        }
        cout << endl;
        //--------------------------stack<vector<int>> sv(d);-----------------------------------------------
        deque<vector<int>> d;
        vector<int> vi;
        vector<vector<int>> vv;
        for (int i = 0; i < 5; ++i)
        {
            for (int j = 0; j < 8; ++j)
                vi.push_back(i * 8 + j);
            vv.push_back(v);
        }
    
        for (auto c : vv)
            d.push_back(c);
    
        stack<vector<int>> sv(d);
        while (!s1.empty())
        {
            diapVector(s1.top());
            s1.pop();
        }
        cout << endl;
        //-------------------------------------------------------------------------
    
        return 0;
    }

    转载自:https://blog.csdn.net/qq_29567701/article/details/79893705

  • 相关阅读:
    JS设计模式之----单例模式
    回流(reflow)与重绘(repaint)
    React native 图标使用
    JS常用几种存储方式的使用规则与各自特征
    Vue
    Promise 一自我总结
    三栏布局 && 两栏布局
    linux限制用户目录
    wireshark 抓包过滤
    python之tomcat自动化备份,更新
  • 原文地址:https://www.cnblogs.com/oneDongHua/p/14264055.html
Copyright © 2011-2022 走看看