1. 程式人生 > >《C++ primer 》 獵豹網校 順序容器 STL deque類 2018/10/7

《C++ primer 》 獵豹網校 順序容器 STL deque類 2018/10/7

  • deque是一個動態陣列
  • deque與vector非常類似
  • deque可以在陣列開頭和末尾插入和刪除資料
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

int main()
{
    deque<int> a;

    a.push_back(3);
    a.push_back(4);
    a.push_back(5);

    a.push_front(2);
    a.push_front(1);
    a.push_front(0);


    for(size_t nCount = 0;nCount <a.size();++nCount)
    {
        cout <<"a[" <<nCount <<"] = " <<a[nCount] <<endl;
    }
    cout << endl;
    cout << endl;
    a.pop_front();
    a.pop_back();

   /* for(size_t nCount = 0;nCount <a.size();++nCount)
    {
        cout <<"a[" <<nCount <<"] = " <<a[nCount] <<endl;
    }*/

    deque<int>::iterator iElementLocater;
    for(iElementLocater = a.begin();iElementLocater !=a.end();++iElementLocater)
    {
        size_t nOffset = distance(a.begin(),iElementLocater);
        cout << "a[" << nOffset << "] ="<<*iElementLocater <<endl;
    }
    cout << endl;

    cout << "Hello world!" << endl;
    return 0;
}

結果輸出:

a[0] = 0 a[1] = 1 a[2] = 2 a[3] = 3 a[4] = 4 a[5] = 5

a[0] =1 a[1] =2 a[2] =3 a[3] =4