zoukankan      html  css  js  c++  java
  • Deque 容器

    1、Deque

    (1)定义

    deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容。deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中某一位置上的操作所花费的是线性时间。与vector不同的是,deque还支持从开始端插入数据:push_front()。

    (2)底层实现

    deque 即双端队列。是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。

    双端队列是限定插入和删除操作在表的两端进行的线性表。这两端分别称做端点1和端点2。也可像栈一样,可以用一个铁道转轨网络来比喻双端队列。在实际使用中,还可以有输出受限的双端队列(即一个端点允许插入和删除,另一个端点只允许插入的双端队列)和输入受限的双端队列(即一个端点允许插入和删除,另一个端点只允许删除的双端队列)。而如果限定双端队列从某个端点插入的元素只能从该端点删除,则该双端队列就蜕变为两个栈底相邻的栈了。

    (3)使用

    使用deque容器之前必须加上<deque>头文件:#include<deuqe>。

    deque属于std命名域的内容,因此需要通过命名限定:using std::deque;也可以直接使用全局的命名空间方式:using namespace std。

    (4)特点

     

    优点:(1)随机访问,即[]操作和deque.at();

              (2)插入与删除方便;

              (3)可在双端进行pop,push。

    缺点:占用内存多。

    2、成员函数

    (1)构造函数

    /*构造函数*/
    deque<Elem> c 
    //创建一个空的deque
    deque<Elem> c1(c2) 
    //复制一个deque。
    deque<Elem> c(n) 
    //创建一个deque,含有n个数据,数据均已缺省构造产生。
    deque<Elem> c(n, elem) 
    //创建一个含有n个elem拷贝的deque。
    deque<Elem> c(beg,end) 
    //创建一个以[beg;end)区间的deque。
    ~deque<Elem>() 
    //销毁所有数据,释放内存。

    (2)赋值操作

    c.assign(n,num);
    //将n个num拷贝复制到容器c
    c.assign(beg,end)将[beg,end);
    //区间的数据拷贝复制到容器c

    示例:

    deque<int> d1 {1,2,3,4,5},d2;
        d2.assign(2, 8);
        deque<int>::iterator it;
        cout << "d2.assign(n,num):";
        for(it=d2.begin();it!=d2.end();it++){
            cout << *it << " ";
        }
        d2.assign(d1.begin(), d1.begin()+3);
        cout << "d2.assign(beg,end):";
        for(it=d2.begin();it!=d2.end();it++){
            cout << *it << " ";
        }
        cout << endl;

    (3)插入操作

    c.push_back(num)在末尾位置插入元素
    c.pop_back()删除末尾位置的元素
    c.push_front(num)在开头位置插入元素
    c.pop_front()删除开头位置的元素
    
    c.insert(pos,num)在pos位置插入元素num
    c.insert(pos,n,num)在pos位置插入n个元素num
    c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素

    示例:

    deque<int> d {1,2,3,4,5};
        deque<int>::iterator it;
        cout << "insert before:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        d.insert(d.end(),22);
        d.insert(d.end(), 3,88);
        int a[5] = {1,2,3,4,5};
        d.insert(d.begin(),a,a+3);
        cout << "insert after:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
    
    deque<int> d {1,2,3,4,5};
        d.push_back(10);
        deque<int>::iterator it;
        cout << "push_back(num):" ;
        for(it=d.begin();it!=d.end();it++){
                cout << *it << " ";
        }
        cout << endl;
        
        d.pop_back();
        cout << "pop_back(num):" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        
        d.push_front(10);
        cout << "push_front(num):" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        
        d.pop_front();
        cout << "pop_front(num):" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        return 0;

    (4)删除操作

    c.clear()清除c容器中拥有的所有元素
    
    c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素
    c.erase(beg,end)删除区间为[beg,end)之间的元素

    示例:

    deque<int> d {1,2,3,4,5};
        deque<int>::iterator it;
        cout << "clear before:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        d.clear();
        cout << "clear after:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
    
    deque<int> d {1,2,3,4,5};
        d.erase(d.begin());
        deque<int>::iterator it;
        cout << "erase(pos) after:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        d.erase(d.begin(), d.begin()+3);
        cout << "erase(beg,end) after:" ;
        for(it=d.begin();it!=d.end();it++){
            cout << *it << " ";
        }
        cout << endl;

    (5)定位操作

    c.begin()返回指向第一个元素的迭代器
    c.end()返回指向最后一个元素下一个位置的迭代器
    
    c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)
    c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)
    
    c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
    
    c.front()返回c容器的第一个元素
    c.back()返回c容器的最后一个元素

    示例:

    deque<int> d {1,2,3,4,5};
     deque<int>::iterator it;
     for(it=d.begin();it!=d.end();it++){
     cout << *it << " ";
     }
    cout << endl;
    
    deque<int> d {1,2,3,4,5};
    deque<int>::reverse_iterator it;
    for(it=d.rbegin();it!=d.rend();it++){
    cout << *it << " ";
    }
    cout << endl;
    
    deque<int> d {1,2,3,4,5};
    cout << "d.at(pos):" << d.at(2);
    return 0;
    
    deque<int> d {1,2,3,4,5};
    if(!d.empty()){
        cout << "d.front():" << d.front() << endl;
        cout << "d.back(): " << d.back() << endl;
      }

    (6)数据大小

    c.empty()判断c容器是否为空
    c.size()返回c容器中实际拥有的元素个数
    c.max_size()返回c容器可能存放元素的最大数量
    c.resize(num)从新定义容器的大小

    示例:

    deque<int> d {1,2,3,4,5};
        if(!d.empty()){
            cout << "d is not empty!" << endl;
        }else{
            cout << "d is empty!" << endl;
        }
        return 0;
    
    deque<int> d {1,2,3,4,5};
    cout << "d.size():" << d.size() << endl;
    return 0;
    
    deque<int> d {1,2,3,4,5};
    cout << "d.max_size():" << d.max_size() << endl;
    return 0;
    
    deque<int> d {1,2,3,4,5};
        cout << "d.size():" << d.size() << endl;
        d.resize(d.size()+5);
        cout << "d.resize() after:" << d.size() <<endl;
        deque<int>::iterator it;
        cout << "resize() after:" ;
        for(it=d.begin();it!=d.end();it++){
        cout << *it << " ";
        }
        cout << endl;

    (7)交换操作

    c1.swap(c2)交换容器c1,c2;
    swap(c1,c2)同上。

    示例:

    deque<int> d1 {1,2,3,4,5},d2,d3;
        d1.swap(d2);
        deque<int>::iterator it;
        cout << "d1 swap after:" ;
        for(it=d1.begin();it!=d1.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        cout << "d2 swap after:" ;
        for(it=d2.begin();it!=d2.end();it++){
            cout << *it << " ";
        }
        cout << endl;
        
        swap(d3,d2);
        cout << "d3 swap after:" ;
        for(it=d3.begin();it!=d3.end();it++){
            cout << *it << " ";
        }
        cout << endl;

    (8)重载运算符

    operator==
    operator!=
    operator<
    operator<=
    operator>
    operator>=

    示例:

    deque<int> d1 {1,2,3,4,5},d2;
        d2 = d1;
        deque<int>::iterator it;
        for(it=d2.begin();it!=d2.end();it++){
            cout << *it << " ";
        }
        cout << endl;
    

      

     

  • 相关阅读:
    Styling a Flex Button control using embedded fonts(转载)
    ArcSDE Administration Command Reference (来自ARCGIS官方)
    FlexPaper_1.2.1.swc——Flex在线显示PDF文档(使用FlexPaper)感悟
    SDE数据的备份与恢复(转载)
    C#中将dll汇入exe,并加壳(转载)
    WEB页面导出为EXCEL/WORD文档的方法 (转载)
    Property Manager Updated 1.0.0.4 小工具介绍(转载)
    Openbravo 3.0安装过程简述
    Installing "uuidossp" library in Postgres 9.1
    BindingSource的事件触发顺序
  • 原文地址:https://www.cnblogs.com/yedushusheng/p/5519911.html
Copyright © 2011-2022 走看看