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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM