• C++ STL deque


    一、简介

    1. deque(Double Ended Queues,双向队列)和向量很相似,但是它允许在容器头部快速插入和删除(就像在尾部一样)。

    2. 资料网址:http://www.cplusplus.com/reference/deque/deque/

    3.实际上,deque是对vector和list优缺点的结合,它是处于两者之间的,一种优化了的对序列两端元素进行添加和删除操作的基本序列容器。它允许较为快速地随机访问,但它不像vector把所有的对象保存在一块连续的内存块,而是采用多个连续的存储块,并且在一个映射结构中保存对这些块及其顺序的跟踪。向deque两端添加或删除元素的开销很小。它不需要重新分配空间,所以向末端增加元素比vector更有效。
    特点:
    (1) 随机访问方便,即支持[] 操作符和vector.at(),但性能没有vector好;
    (2) 可以在内部进行插入和删除操作,但性能不及list;
    (3) 可以在两端进行入列出列操作;
    (4) 相对于verctor占用更多的内存。

    二、例子

    1. 测试

    #include <deque>
    #include <iostream>
    #include <algorithm>
    #include <stdexcept>
    using namespace std;
      
    void print(int num)
    { 
        cout << num << " ";
    } 
      
    int main()
    { 
        //1. 初始化 
        deque<int> v;
        deque<int>::iterator iv;
    
        v.assign(10, 2); //将10个值为2的元素赋到deque中
        cout << v.size() << endl; //返回deque实际含有的元素数量
        cout << "-------------1--------------" << endl;
    
        //2. 添加
        v.push_front(666);
        for (int i = 0; i < 10; i++) {
            v.push_back(i);
        }
        for_each(v.begin(), v.end(), print); //需要#include <algorithm> 
        cout << endl; 
        cout << v.size() << endl; 
        cout << "-------------2--------------" << endl;
    
        //3. 插入及遍历、逆遍历 
        v.insert(v.begin() + 3, 99); 
        v.insert(v.end() - 3, 99); 
        for_each(v.begin(), v.end(), print); 
        cout << endl; 
        for_each(v.rbegin(), v.rend(), print); //在逆序迭代器上做++运算将指向容器中的前一个元素 
        cout << endl; 
        cout << "-------------3--------------" << endl;
    
        //一般遍历写法
        for(iv = v.begin(); iv != v.end(); ++iv) {
            cout << *iv << " ";
        }
        cout << endl;
        cout << "-------------4--------------" << endl;
    
        //4. 删除 
        v.erase(v.begin() + 3); 
        for_each(v.begin(), v.end(), print); 
        cout << endl; 
        v.insert(v.begin() + 3, 99);//还原 
    
        v.erase(v.begin(), v.begin() + 3); //注意删除了3个元素而不是4个 
        for_each(v.begin(), v.end(), print); 
        cout << endl; 
        cout << "-------------5--------------" << endl;
    
        v.pop_front();
        v.pop_back();
        for_each(v.begin(), v.end(), print);
        cout << endl;
        cout << "-------------6--------------" << endl;
    
        //5. 查询
        cout << v.front() << endl; 
        cout << v.back() << endl;
        cout << "-------------7--------------" << endl;
    
        //危险的做法,但一般我们就像访问数组那样操作就行 
        for (int i = 15; i < 25; i++){
            cout << "Element " << i << " is " << v[i] << endl;     
        }
        cout << "-------------8--------------" << endl;
    
        //安全的做法 
        int i; 
        try { 
            for (i = 15; i < 25; i++) {
              cout << "Element " << i << " is " << v.at(i) << endl;
            }
        } catch (out_of_range err) {//#include <stdexcept> 
            cout << "out_of_range at " << i << endl;
        }
        cout << "-------------9--------------" << endl;
    
        //6. 清空 
        v.clear(); 
        cout << v.size() << endl;//0 
        for_each(v.begin(), v.end(), print); //已经clear,v.begin()==v.end(),不会有任何结果。
        cout << "-------------10--------------" << endl;
    
        return 0; 
    }
    
    /*
    # ./pp
    10
    -------------1--------------
    666 2 2 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 7 8 9 
    21
    -------------2--------------
    666 2 2 99 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 99 7 8 9 
    9 8 7 99 6 5 4 3 2 1 0 2 2 2 2 2 2 2 2 99 2 2 666 
    -------------3--------------
    666 2 2 99 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 99 7 8 9 
    -------------4--------------
    666 2 2 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 99 7 8 9 
    99 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 99 7 8 9 
    -------------5--------------
    2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 99 7 8 
    -------------6--------------
    2
    8
    -------------7--------------
    Element 15 is 99
    Element 16 is 7
    Element 17 is 8
    Element 18 is 9
    Element 19 is 0
    Element 20 is 0
    Element 21 is 0
    Element 22 is 0
    Element 23 is 0
    Element 24 is 0
    -------------8--------------
    Element 15 is 99
    Element 16 is 7
    Element 17 is 8
    out_of_range at 18
    -------------9--------------
    0
    -------------10--------------
    
    */
  • 相关阅读:
    【转】【VS2008无法启动asp.net development server】的解决
    C#运用技巧(1)
    C# — WinForm 基本控件
    TB 需求分析
    C# 远程连接SQL 2005数据库
    SQL语句的运用
    如何跌倒
    国学堂-梁冬对话张长琳《人体的彩虹》系列
    帝范:中国最伟大帝王的沉思录
    web.xml 配置
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/14589439.html
Copyright © 2020-2023  润新知