C++/C++11中std::deque的使用(转)

std::deque是双端队列,可以高效的在头尾两端插入和删除元素,在std::deque两端插入和删除并不会使其它元素的指针或引用失效。在接口上和std::vector相似。与sdk::vector相反,std::deque中的元素并非连续存储:典型的实现是使用一个单独分配的固定大小数组的序列。std::deque的存储空间会自动按需扩大和缩小。扩大std::deque比扩大std::vector要便宜,因为它不涉及到现有元素复制到新的内存位置。

双端队列(Double-ended queue,缩写为Deque)是一个大小可以动态变化(Dynamic size)且可以在两端扩展或收缩的顺序容器。顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。不同的库可能会按不同的方式来实现双端队列,通常实现为某种形式的动态数组。但不管通过哪种方式,双端队列都允许通过随机迭代器直接访问各个元素,且内部的存储空间会按需求自动地扩展或收缩。容器实际分配的内存数超过容纳当前所有有效元素所需的,因为额外的内存将被未来增长的部分所使用。就因为这点,当插入元素时,容器不需要太频繁地分配内存。因此,双端队列提供了类似向量(std::vector)的功能,且不仅可以在容器末尾,还可以在容器开头高效地插入或删除元素。但是,相比向量,双端队列不保证内部的元素是按连续的存储空间存储的,因此,不允许对指针直接做偏移操作来直接访问元素。在内部,双端队列与向量的工作方式完全不同:向量使用单数组数据结构,在元素增加的过程中,需要偶尔的内存重分配,而双端队列中的元素被零散地保存在不同的存储块中,容器内部会保存一些必要的数据使得可以以恒定时间及一个统一的顺序接口直接访问任意元素。因此,双端队列的内部实现比向量的稍稍复杂一点,但这也使得它在一些特定环境下可以更高效地增长,特别是对于非常长的序列,内存重分配的代价是及其高昂的。对于大量涉及在除了起始或末尾以外的其它任意位置插入或删除元素的操作,相比列表(std::list)及正向列表(std::forward_list),deque 所表现出的性能是极差的,且操作前后的迭代器、引用的一致性较低。

A deque is very much like a vector: like vector, it is a sequence that supports random access to elements, constant time insertion and removal of elements at the end of these quence, and linear time insertion and removal of elements in the middle. The main way in which deque differs from vector is that deque also supports constant time insertion and removal of elements at the beginning of the sequence. Additionally, deque does not have any member functions analogous to vector's capacity() and reserve(), and does not provide any of the guarantees on iterator validity that are associated with those member functions.

 

一个容器就是一些特定类型对象的集合。顺序容器(sequential container)为程序员提供了控制元素存储和访问顺序的能力。这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应。

         标准库中的顺序容器包括:

         (1)、vector:可变大小数组。支持快速随机访问。在尾部之外的位置插入或删除元素可能很慢。

         (2)、deque:双端队列。支持快速随机访问。在头尾位置插入/删除速度很快。

         (3)、list:双向链表。只支持双向顺序访问。在list中任何位置进行插入/删除操作速度都很快。

         (4)、forward_list:单向链表。只支持单向顺序访问。在链表任何位置进行插入/删除操作速度都很快。

         (5)、array:固定大小数组。支持快速随机访问。不能添加或删除元素。

         (6)、string:与vector相似的容器,但专门用于保存字符。随机访问快。在尾部插入/删除速度快。

         除了固定大小的array外,其它容器都提供高效、灵活的内存管理。我们可以添加和删除元素,扩张和收缩容器的大小。容器保存元素的策略对容器操作的效率有着固定的,有时是重大的影响。在某些情况下,存储策略还会影响特定容器是否支持特定操作。

         例如,string和vector将元素保存在连续的内存空间中。由于元素是连续存储的,由元素的下标来计算其地址是非常快速的。但是,在这两种容器的中间位置添加或删除元素就会非常耗时:在一次插入或删除操作后,需要移动插入/删除位置之后的所有元素,来保持连续存储。而且,添加一个元素有时可能还需要分配额外的存储空间。在这种情况下,每个元素都必须移动到新的存储空间中。

         list和forward_list两个容器的设计目的是令容器任何位置的添加和删除操作都很快速。作为代价,这两个容器不支持元素的随机访问:为了访问一个元素,我们只能遍历整个容器。而且,与vector、deque和array相比,这两个容器的额外内存开销也很大。

         deque是一个更为复杂的数据结构。与string和vector类似,deque支持快速的随机访问。与string和vector一样,在deque的中间位置添加或删除元素的代价(可能)很高。但是,在deque的两端添加或删除元素都是很快的,与list或forward_list添加删除元素的速度相当。

         forward_list和array是新C++标准增加的类型。与内置数组相比,array是一个种更安全、更容易使用的数组类型。与内置数组类似,array对象的大小是固定的。因此,array不支持添加和删除元素以及改变容器大小的操作。forward_list的设计目标是达到与最好的手写的单向链表数据结构相当的性能。因此,forward_list没有size操作,因为保存或计算其大小就会比手写链表多出额外的开销。对其他容器而言,size保证是一个快速的常量时间的操作。

         通常,使用vector是最好的选择,除法你有很好的理由选择其他容器。

         以下是一些选择容器的基本原则:

         (1)、除法你有很好的理由选择其他容器,否则应该使用vector;

         (2)、如果你的程序有很多小的元素,且空间的额外开销很重要,则不要使用list或forward_list;

         (3)、如果程序要求随机访问元素,应使用vector或deque;

         (4)、如果程序要求在容器的中间插入或删除元素,应使用list或forward_list;

(5)、如果程序需要在头尾位置插入或删除元素,但不会在中间位置进行插入或删除操作,则使用deque;

(6)、如果程序只有在读取输入时才需要在容器中间位置插入元素,随后需要随机访问元素,则:首先,确定是否真的需要在容器中间位置添加元素。当处理输入数据时,通常可以很容器地向vector追加数据,然后再调用标准库的sort函数来重排容器中的元素,从而避免在中间位置添加元素。如果必须在中间位置插入元素,考虑在输入阶段使用list,一旦输入完成,将list中的内容拷贝到一个vector中。

如果你不确定应该使用哪种容器,那么可以在程序中只使用vector和list公共的操作:使用迭代器,不使用下标操作,避免随机访问。这样,在必要时选择使用vector或list都很方便。

一般来说,每个容器都定义在一个头文件中,文件名与类型名相同。即,deque定义在头文件deque中,list定义在头文件list中,以此类推。容器均定义为模板类。

顺序容器几乎可以保存任意类型的元素。特别是,我们可以定义一个容器,其元素的类型是另一个容器。这种容器的定义与任何其他容器类型完全一样:在尖括号中指定元素类型(此种情况下,是另一种容器类型)。

除了顺序容器外,标准库还定义了三个顺序容器适配器:stack、queue和priority_queue。适配器(adaptor)是标准库中的一个通用概念。容器、迭代器和函数都有适配器。本质上,一个适配器是一种机制,能使某种事物的行为看起来像另外一种事物一样。一个容器适配器接受一种已有的容器类型,使其行为看起来像一种不同的类型。

 

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

 

  1.  
    #include "deque.hpp"
  2.  
    #include <iostream>
  3.  
    #include <deque>
  4.  
    #include <vector>
  5.  
     
  6.  
    ////////////////////////////////////////////////////////
  7.  
    // https://msdn.microsoft.com/en-us/library/22a9t119.aspx
  8.  
    int test_deque_2()
  9.  
    {
  10.  
    { // deque::at: Returns a reference to the element at a specified location in the deque.
  11.  
    using namespace std;
  12.  
    deque <int> c1;
  13.  
     
  14.  
    c1.push_back(10);
  15.  
    c1.push_back(20);
  16.  
     
  17.  
    const int& i = c1.at(0);
  18.  
    int& j = c1.at(1);
  19.  
    cout << "The first element is " << i << endl;
  20.  
    cout << "The second element is " << j << endl;
  21.  
    }
  22.  
     
  23.  
    { // deque::back: Returns a reference to the last element of the deque.
  24.  
    using namespace std;
  25.  
    deque <int> c1;
  26.  
     
  27.  
    c1.push_back(10);
  28.  
    c1.push_back(11);
  29.  
     
  30.  
    int& i = c1.back();
  31.  
    const int& ii = c1.front();
  32.  
     
  33.  
    cout << "The last integer of c1 is " << i << endl; // 11
  34.  
    i--;
  35.  
    cout << "The next-to-last integer of c1 is " << ii << endl; // 10
  36.  
    cout << "The last integer of c1 is " << c1.back() << endl; // 10
  37.  
    }
  38.  
     
  39.  
    { // deque::clear: Erases all the elements of a deque.
  40.  
    using namespace std;
  41.  
    deque <int> c1;
  42.  
     
  43.  
    c1.push_back(10);
  44.  
    c1.push_back(20);
  45.  
    c1.push_back(30);
  46.  
     
  47.  
    cout << "The size of the deque is initially " << c1.size() << endl;
  48.  
    c1.clear();
  49.  
    cout << "The size of the deque after clearing is " << c1.size() << endl;
  50.  
    }
  51.  
     
  52.  
    { // deque::const_reference: A type that provides a reference to a const element stored in a deque for reading and performing const operations
  53.  
    using namespace std;
  54.  
    deque <int> c1;
  55.  
     
  56.  
    c1.push_back(10);
  57.  
    c1.push_back(20);
  58.  
     
  59.  
    const deque <int> c2 = c1;
  60.  
    const int &i = c2.front();
  61.  
    const int &j = c2.back();
  62.  
    cout << "The first element is " << i << endl;
  63.  
    cout << "The second element is " << j << endl;
  64.  
     
  65.  
    // The following line would cause an error as c2 is const
  66.  
    // c2.push_back( 30 );
  67.  
    }
  68.  
     
  69.  
    { // deque::crbegin: Returns a const iterator to the first element in a reversed deque
  70.  
    using namespace std;
  71.  
    deque <int> v1;
  72.  
    deque <int>::iterator v1_Iter;
  73.  
    deque <int>::const_reverse_iterator v1_rIter;
  74.  
     
  75.  
    v1.push_back(1);
  76.  
    v1.push_back(2);
  77.  
     
  78.  
    v1_Iter = v1.begin();
  79.  
    cout << "The first element of deque is "
  80.  
    << *v1_Iter << "." << endl;
  81.  
     
  82.  
    v1_rIter = v1.crbegin();
  83.  
    cout << "The first element of the reversed deque is "
  84.  
    << *v1_rIter << "." << endl;
  85.  
    }
  86.  
     
  87.  
    { // deque::emplace: Inserts an element constructed in place into the deque at a specified position.
  88.  
    using namespace std;
  89.  
    deque <int> v1;
  90.  
    deque <int>::iterator Iter;
  91.  
     
  92.  
    v1.push_back(10);
  93.  
    v1.push_back(20);
  94.  
    v1.push_back(30);
  95.  
     
  96.  
    cout << "v1 =";
  97.  
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
  98.  
    cout << " " << *Iter;
  99.  
    cout << endl;
  100.  
     
  101.  
    // initialize a deque of deques by moving v1
  102.  
    deque < deque <int> > vv1;
  103.  
     
  104.  
    vv1.emplace(vv1.begin(), move(v1));
  105.  
    if (vv1.size() != 0 && vv1[0].size() != 0) {
  106.  
    cout << "vv1[0] =";
  107.  
    for (Iter = vv1[0].begin(); Iter != vv1[0].end(); Iter++)
  108.  
    cout << " " << *Iter;
  109.  
    cout << endl;
  110.  
    }
  111.  
    }
  112.  
     
  113.  
    return 0;
  114.  
    }
  115.  
     
  116.  
    /////////////////////////////////////////////////////////////
  117.  
    // reference: http://www.cplusplus.com/reference/deque/deque/
  118.  
    int test_deque_1()
  119.  
    {
  120.  
    { // deque::deque: Construct deque container
  121.  
    unsigned int i;
  122.  
     
  123.  
    // constructors used in the same order as described above:
  124.  
    std::deque<int> first; // empty deque of ints
  125.  
    std::deque<int> second(4, 100); // four ints with value 100
  126.  
    std::deque<int> third(second.begin(), second.end()); // iterating through second
  127.  
    std::deque<int> fourth(third); // a copy of third
  128.  
     
  129.  
    // the iterator constructor can be used to copy arrays:
  130.  
    int myints[] = { 16, 2, 77, 29 };
  131.  
    std::deque<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
  132.  
     
  133.  
    std::cout << "The contents of fifth are:";
  134.  
    for (std::deque<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
  135.  
    std::cout << ' ' << *it; // 16 2 77 29
  136.  
    std::cout << '\n';
  137.  
    }
  138.  
     
  139.  
    { // deque::assign: Assigns new contents to the deque container,
  140.  
    // replacing its current contents, and modifying its size accordingly.
  141.  
     
  142.  
    std::deque<int> first;
  143.  
    std::deque<int> second;
  144.  
    std::deque<int> third;
  145.  
     
  146.  
    first.assign(7, 100); // 7 ints with a value of 100
  147.  
     
  148.  
    std::deque<int>::iterator it;
  149.  
    it = first.begin() + 1;
  150.  
     
  151.  
    second.assign(it, first.end() - 1); // the 5 central values of first
  152.  
     
  153.  
    int myints[] = { 1776, 7, 4 };
  154.  
    third.assign(myints, myints + 3); // assigning from array.
  155.  
     
  156.  
    std::cout << "Size of first: " << int(first.size()) << '\n'; // 7
  157.  
    std::cout << "Size of second: " << int(second.size()) << '\n'; // 5
  158.  
    std::cout << "Size of third: " << int(third.size()) << '\n'; // 3
  159.  
    }
  160.  
     
  161.  
    { // deque::at: Returns a reference to the element at position n in the deque container object.
  162.  
    std::deque<unsigned> mydeque(10); // 10 zero-initialized unsigneds
  163.  
     
  164.  
    // assign some values:
  165.  
    for (unsigned i = 0; i < mydeque.size(); i++)
  166.  
    mydeque.at(i) = i;
  167.  
     
  168.  
    std::cout << "mydeque contains:";
  169.  
    for (unsigned i = 0; i < mydeque.size(); i++)
  170.  
    std::cout << ' ' << mydeque.at(i); // 0 1 2 3 4 5 6 7 8 9
  171.  
    std::cout << '\n';
  172.  
    }
  173.  
     
  174.  
    { // deque::back: Returns a reference to the last element in the container.
  175.  
    // deque::push_back: Adds a new element at the end of the deque container,
  176.  
    // after its current last element. The content of val is copied (or moved) to the new element
  177.  
    std::deque<int> mydeque;
  178.  
    mydeque.push_back(10);
  179.  
     
  180.  
    while (mydeque.back() != 0)
  181.  
    mydeque.push_back(mydeque.back() - 1);
  182.  
     
  183.  
    std::cout << "mydeque contains:";
  184.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  185.  
    std::cout << ' ' << *it;
  186.  
    std::cout << '\n';
  187.  
    }
  188.  
     
  189.  
    { // deque::begin: Return iterator to beginning
  190.  
    // deque::end: Return iterator to end
  191.  
    std::deque<int> mydeque;
  192.  
    for (int i = 1; i <= 5; i++) mydeque.push_back(i);
  193.  
     
  194.  
    std::cout << "mydeque contains:";
  195.  
    std::deque<int>::iterator it = mydeque.begin();
  196.  
     
  197.  
    while (it != mydeque.end())
  198.  
    std::cout << ' ' << *it++;
  199.  
    std::cout << '\n';
  200.  
    }
  201.  
     
  202.  
    { // deque::cbegin: c++11, Return const_iterator to beginning
  203.  
    // deque::cend: c++11, Return const_iterator to end
  204.  
    std::deque<int> mydeque = { 10, 20, 30, 40, 50 };
  205.  
     
  206.  
    std::cout << "mydeque contains:";
  207.  
    for (auto it = mydeque.cbegin(); it != mydeque.cend(); ++it)
  208.  
    std::cout << ' ' << *it;
  209.  
    std::cout << '\n';
  210.  
    }
  211.  
     
  212.  
    { // deque::clear: Clear content
  213.  
    unsigned int i;
  214.  
    std::deque<int> mydeque;
  215.  
    mydeque.push_back(100);
  216.  
    mydeque.push_back(200);
  217.  
    mydeque.push_back(300);
  218.  
     
  219.  
    std::cout << "mydeque contains:";
  220.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  221.  
    std::cout << ' ' << *it;
  222.  
    std::cout << '\n';
  223.  
     
  224.  
    mydeque.clear();
  225.  
    mydeque.push_back(1101);
  226.  
    mydeque.push_back(2202);
  227.  
     
  228.  
    std::cout << "mydeque contains:";
  229.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  230.  
    std::cout << ' ' << *it;
  231.  
    std::cout << '\n';
  232.  
    }
  233.  
     
  234.  
    { // deque::crbegin: c++11, Return const_reverse_iterator to reverse beginning
  235.  
    // deque::crend: c++11, Return const_reverse_iterator to reverse end
  236.  
    std::deque<int> mydeque = { 1, 2, 3, 4, 5 };
  237.  
     
  238.  
    std::cout << "mydeque backwards:";
  239.  
    for (auto rit = mydeque.crbegin(); rit != mydeque.crend(); ++rit)
  240.  
    std::cout << ' ' << *rit;
  241.  
    std::cout << '\n';
  242.  
    }
  243.  
     
  244.  
    { // deque::emplace: c++11, Construct and insert element
  245.  
    std::deque<int> mydeque = { 10, 20, 30 };
  246.  
     
  247.  
    auto it = mydeque.emplace(mydeque.begin() + 1, 100);
  248.  
    mydeque.emplace(it, 200);
  249.  
    mydeque.emplace(mydeque.end(), 300);
  250.  
     
  251.  
    std::cout << "mydeque contains:";
  252.  
    for (auto& x : mydeque)
  253.  
    std::cout << ' ' << x;
  254.  
    std::cout << '\n';
  255.  
    }
  256.  
     
  257.  
    { // deque::emplace_back: c++11, Construct and insert element at the end
  258.  
    std::deque<int> mydeque = { 10, 20, 30 };
  259.  
     
  260.  
    mydeque.emplace_back(100);
  261.  
    mydeque.emplace_back(200);
  262.  
     
  263.  
    std::cout << "mydeque contains:";
  264.  
    for (auto& x : mydeque)
  265.  
    std::cout << ' ' << x;
  266.  
    std::cout << '\n';
  267.  
    }
  268.  
     
  269.  
    { // deque::emplace_front: c++11, Construct and insert element at beginning
  270.  
    std::deque<int> mydeque = { 10, 20, 30 };
  271.  
     
  272.  
    mydeque.emplace_front(111);
  273.  
    mydeque.emplace_front(222);
  274.  
     
  275.  
    std::cout << "mydeque contains:";
  276.  
    for (auto& x : mydeque)
  277.  
    std::cout << ' ' << x;
  278.  
    std::cout << '\n';
  279.  
    }
  280.  
     
  281.  
    { // deque::empty: Test whether container is empty
  282.  
    std::deque<int> mydeque;
  283.  
    int sum(0);
  284.  
     
  285.  
    for (int i = 1; i <= 10; i++) mydeque.push_back(i);
  286.  
     
  287.  
    while (!mydeque.empty()) {
  288.  
    sum += mydeque.front();
  289.  
    mydeque.pop_front();
  290.  
    }
  291.  
     
  292.  
    std::cout << "total: " << sum << '\n';
  293.  
    }
  294.  
     
  295.  
    { // deque::erase: Erase elements
  296.  
    std::deque<int> mydeque;
  297.  
     
  298.  
    // set some values (from 1 to 10)
  299.  
    for (int i = 1; i <= 10; i++) mydeque.push_back(i);
  300.  
     
  301.  
    // erase the 6th element
  302.  
    mydeque.erase(mydeque.begin() + 5);
  303.  
     
  304.  
    // erase the first 3 elements:
  305.  
    mydeque.erase(mydeque.begin(), mydeque.begin() + 3);
  306.  
     
  307.  
    std::cout << "mydeque contains:";
  308.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  309.  
    std::cout << ' ' << *it;
  310.  
    std::cout << '\n';
  311.  
    }
  312.  
     
  313.  
    { // deque::front: Access first element, Returns a reference to the first element in the deque containe
  314.  
    // deque::push_front: Inserts a new element at the beginning of the deque container,
  315.  
    // right before its current first element. The content of val is copied (or moved) to the inserted element
  316.  
    std::deque<int> mydeque;
  317.  
     
  318.  
    mydeque.push_front(77);
  319.  
    mydeque.push_back(20);
  320.  
     
  321.  
    mydeque.front() -= mydeque.back();
  322.  
     
  323.  
    std::cout << "mydeque.front() is now " << mydeque.front() << '\n';
  324.  
    }
  325.  
     
  326.  
    { // deque::get_allocator: Returns a copy of the allocator object associated with the deque object
  327.  
    std::deque<int> mydeque;
  328.  
    int * p;
  329.  
    unsigned int i;
  330.  
     
  331.  
    // allocate an array with space for 5 elements using deque's allocator:
  332.  
    p = mydeque.get_allocator().allocate(5);
  333.  
     
  334.  
    // construct values in-place on the array:
  335.  
    for (i = 0; i < 5; i++) mydeque.get_allocator().construct(&p[i], i);
  336.  
     
  337.  
    std::cout << "The allocated array contains:";
  338.  
    for (i = 0; i < 5; i++) std::cout << ' ' << p[i];
  339.  
    std::cout << '\n';
  340.  
     
  341.  
    // destroy and deallocate:
  342.  
    for (i = 0; i < 5; i++) mydeque.get_allocator().destroy(&p[i]);
  343.  
    mydeque.get_allocator().deallocate(p, 5);
  344.  
    }
  345.  
     
  346.  
    { // deque::insert: Insert elements
  347.  
    std::deque<int> mydeque;
  348.  
     
  349.  
    // set some initial values:
  350.  
    for (int i = 1; i < 6; i++) mydeque.push_back(i); // 1 2 3 4 5
  351.  
     
  352.  
    std::deque<int>::iterator it = mydeque.begin();
  353.  
    ++it;
  354.  
     
  355.  
    it = mydeque.insert(it, 10); // 1 10 2 3 4 5
  356.  
    // "it" now points to the newly inserted 10
  357.  
     
  358.  
    mydeque.insert(it, 2, 20); // 1 20 20 10 2 3 4 5
  359.  
    // "it" no longer valid!
  360.  
     
  361.  
    it = mydeque.begin() + 2;
  362.  
     
  363.  
    std::vector<int> myvector(2, 30);
  364.  
    mydeque.insert(it, myvector.begin(), myvector.end());
  365.  
    // 1 20 30 30 20 10 2 3 4 5
  366.  
     
  367.  
    std::cout << "mydeque contains:";
  368.  
    for (it = mydeque.begin(); it != mydeque.end(); ++it)
  369.  
    std::cout << ' ' << *it;
  370.  
    std::cout << '\n';
  371.  
    }
  372.  
     
  373.  
    { // deque::max_size: Return maximum size
  374.  
    unsigned int i;
  375.  
    std::deque<int> mydeque;
  376.  
     
  377.  
    std::cout << "Enter number of elements: ";
  378.  
    i = 100; //std::cin >> i;
  379.  
     
  380.  
    if (i < mydeque.max_size()) mydeque.resize(i);
  381.  
    else std::cout << "That size exceeds the limit.\n";
  382.  
    fprintf(stderr, "max size: %d\n", mydeque.max_size());
  383.  
    }
  384.  
     
  385.  
    { // deque::operator=: Assigns new contents to the container, replacing its current contents, and modifying its size accordingly
  386.  
    std::deque<int> first(3); // deque with 3 zero-initialized ints
  387.  
    std::deque<int> second(5); // deque with 5 zero-initialized ints
  388.  
     
  389.  
    second = first;
  390.  
    first = std::deque<int>();
  391.  
     
  392.  
    std::cout << "Size of first: " << int(first.size()) << '\n';
  393.  
    std::cout << "Size of second: " << int(second.size()) << '\n';
  394.  
    }
  395.  
     
  396.  
    { // deque::operator[]: Returns a reference to the element at position n in the deque container
  397.  
    std::deque<int> mydeque(10); // 10 zero-initialized elements
  398.  
    std::deque<int>::size_type sz = mydeque.size();
  399.  
     
  400.  
    // assign some values:
  401.  
    for (unsigned i = 0; i < sz; i++) mydeque[i] = i;
  402.  
     
  403.  
    // reverse order of elements using operator[]:
  404.  
    for (unsigned i = 0; i < sz / 2; i++) {
  405.  
    int temp;
  406.  
    temp = mydeque[sz - 1 - i];
  407.  
    mydeque[sz - 1 - i] = mydeque[i];
  408.  
    mydeque[i] = temp;
  409.  
    }
  410.  
     
  411.  
    // print content:
  412.  
    std::cout << "mydeque contains:";
  413.  
    for (unsigned i = 0; i < sz; i++)
  414.  
    std::cout << ' ' << mydeque[i];
  415.  
    std::cout << '\n';
  416.  
    }
  417.  
     
  418.  
    { // deque::pop_back: Removes the last element in the deque container, effectively reducing the container size by one
  419.  
    std::deque<int> mydeque;
  420.  
    int sum(0);
  421.  
    mydeque.push_back(10);
  422.  
    mydeque.push_back(20);
  423.  
    mydeque.push_back(30);
  424.  
     
  425.  
    while (!mydeque.empty()) {
  426.  
    sum += mydeque.back();
  427.  
    mydeque.pop_back();
  428.  
    }
  429.  
     
  430.  
    std::cout << "The elements of mydeque add up to " << sum << '\n';
  431.  
    }
  432.  
     
  433.  
    { // deque::pop_front: Removes the first element in the deque container, effectively reducing its size by one.
  434.  
    std::deque<int> mydeque;
  435.  
     
  436.  
    mydeque.push_back(100);
  437.  
    mydeque.push_back(200);
  438.  
    mydeque.push_back(300);
  439.  
     
  440.  
    std::cout << "Popping out the elements in mydeque:";
  441.  
    while (!mydeque.empty()) {
  442.  
    std::cout << ' ' << mydeque.front();
  443.  
    mydeque.pop_front();
  444.  
    }
  445.  
     
  446.  
    std::cout << "\nThe final size of mydeque is " << int(mydeque.size()) << '\n';
  447.  
    }
  448.  
     
  449.  
    { // deque::rbegin: Returns a reverse iterator pointing to the last element in the container
  450.  
    // deque::rend: Returns a reverse iterator pointing to the theoretical element preceding the first element in the deque container
  451.  
    std::deque<int> mydeque(5); // 5 default-constructed ints
  452.  
     
  453.  
    std::deque<int>::reverse_iterator rit = mydeque.rbegin();
  454.  
     
  455.  
    int i = 0;
  456.  
    for (rit = mydeque.rbegin(); rit != mydeque.rend(); ++rit)
  457.  
    *rit = ++i;
  458.  
     
  459.  
    std::cout << "mydeque contains:";
  460.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  461.  
    std::cout << ' ' << *it;
  462.  
    std::cout << '\n';
  463.  
    }
  464.  
     
  465.  
    { // deque::resize: Resizes the container so that it contains n elements
  466.  
    std::deque<int> mydeque;
  467.  
    std::deque<int>::iterator it;
  468.  
     
  469.  
    // set some initial content:
  470.  
    for (int i = 1; i < 10; ++i) mydeque.push_back(i);
  471.  
     
  472.  
    mydeque.resize(5);
  473.  
    mydeque.resize(8, 100);
  474.  
    mydeque.resize(12);
  475.  
     
  476.  
    std::cout << "mydeque contains:";
  477.  
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  478.  
    std::cout << ' ' << *it;
  479.  
    std::cout << '\n';
  480.  
    }
  481.  
     
  482.  
    { // deque::shrink_to_fit: c++11, Requests the container to reduce its memory usage to fit its size.
  483.  
    // deque::size: Returns the number of elements in the deque container
  484.  
    std::deque<int> mydeque(100);
  485.  
    std::cout << "1. size of mydeque: " << mydeque.size() << '\n';
  486.  
     
  487.  
    mydeque.resize(10);
  488.  
    std::cout << "2. size of mydeque: " << mydeque.size() << '\n';
  489.  
     
  490.  
    mydeque.shrink_to_fit();
  491.  
    fprintf(stderr, "3. size of mydeque: %d\n", mydeque.size());
  492.  
    }
  493.  
     
  494.  
    { // deque::swap: Exchanges the content of the container by the content of x,
  495.  
    // which is another deque object containing elements of the same type. Sizes may differ.
  496.  
    unsigned int i;
  497.  
    std::deque<int> foo(3, 100); // three ints with a value of 100
  498.  
    std::deque<int> bar(5, 200); // five ints with a value of 200
  499.  
     
  500.  
    foo.swap(bar);
  501.  
     
  502.  
    std::cout << "foo contains:";
  503.  
    for (std::deque<int>::iterator it = foo.begin(); it != foo.end(); ++it)
  504.  
    std::cout << ' ' << *it;
  505.  
    std::cout << '\n';
  506.  
     
  507.  
    std::cout << "bar contains:";
  508.  
    for (std::deque<int>::iterator it = bar.begin(); it != bar.end(); ++it)
  509.  
    std::cout << ' ' << *it;
  510.  
    std::cout << '\n';
  511.  
    }
  512.  
     
  513.  
    { // relational operators: compare
  514.  
    std::deque<int> foo(3, 100); // three ints with a value of 100
  515.  
    std::deque<int> bar(2, 200); // two ints with a value of 200
  516.  
     
  517.  
    if (foo == bar) std::cout << "foo and bar are equal\n";
  518.  
    if (foo != bar) std::cout << "foo and bar are not equal\n";
  519.  
    if (foo< bar) std::cout << "foo is less than bar\n";
  520.  
    if (foo> bar) std::cout << "foo is greater than bar\n";
  521.  
    if (foo <= bar) std::cout << "foo is less than or equal to bar\n";
  522.  
    if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";
  523.  
    }
  524.  
     
  525.  
    { // std::swap: The contents of container x are exchanged with those of y.
  526.  
    // Both container objects must be of the same type (same template parameters), although sizes may differ
  527.  
    unsigned int i;
  528.  
    std::deque<int> foo(3, 100); // three ints with a value of 100
  529.  
    std::deque<int> bar(5, 200); // five ints with a value of 200
  530.  
     
  531.  
    swap(foo, bar);
  532.  
     
  533.  
    std::cout << "foo contains:";
  534.  
    for (std::deque<int>::iterator it = foo.begin(); it != foo.end(); ++it)
  535.  
    std::cout << ' ' << *it;
  536.  
    std::cout << '\n';
  537.  
     
  538.  
    std::cout << "bar contains:";
  539.  
    for (std::deque<int>::iterator it = bar.begin(); it != bar.end(); ++it)
  540.  
    std::cout << ' ' << *it;
  541.  
    std::cout << '\n';
  542.  
    }
  543.  
     
  544.  
    return 0;
  545.  
    }

GitHub:https://github.com/fengbingchun/Messy_Test

原文链接: https://www.cnblogs.com/spruce/p/13469702.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++/C++11中std::deque的使用(转)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/201275

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午8:44
下一篇 2023年2月12日 下午8:45

相关推荐