STL中deque
以下學習一下STL中另一種序列容器——deque。
deque表示double-ended queue,即雙向隊列,deque是通過作為動態數組的方式實現的,這樣可以在兩端插入元素。因此,deque可以在任何一個方向進行擴展。同時可以在中間插入元素。在開頭或結尾處插入元素非常的快,然而在中間插入元素將會比較耗時間,因此需要移動隊列中的元素。
定義deque容器的類名為deque。類deque的定義以及deque對象的各種操作函數的實現包含在頭文件<deque>中,因此,在程序中使用deque時,程序中必須包含如下語句:
?
#include <deque>
類deque中包含好幾個構造器,因此,當聲明一個deque對象時,可以通過各種方式進行初始化。如下表中提供的方式:
?
除了之前介紹的所有序列容器通用的操作意外,下表還描述了用來管理deque容器的元素的操作。各個語句展示了如何使用某一個特定的函數,其中假設deq是一個deque容器
下例展示了如何在程序中使用deque。
?
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>using namespace std;int main()
{deque<int> intDeq;ostream_iterator<int> screen(cout, " ");intDeq.push_back(13);intDeq.push_back(75);intDeq.push_back(28);intDeq.push_back(35);cout << "intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;intDeq.push_front(0);intDeq.push_back(100);cout << "After adding two more "<< "elements, one at the front " << endl<< " and one at the back, intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;intDeq.pop_front();intDeq.pop_front();cout << "After removing the first "<< "two elements, " << endl<< " intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;intDeq.pop_back();intDeq.pop_back();cout << "After removing the last "<< "two elements, " << endl<< " intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;deque<int>::iterator deqIt;deqIt = intDeq.begin();++deqIt;intDeq.insert(deqIt, 666);cout << "After inserting 666, "<< "intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;intDeq.assign(2, 45);cout << "After assigning two "<< "copies of 45, intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;intDeq.push_front(-10);intDeq.push_front(-999);cout << "After inserting two "<< "elements, one at the front " << endl<< " and one at the back, intDeq: ";copy(intDeq.begin(), intDeq.end(), screen);cout << endl;return 0;
} ?
輸出為:
?
?
轉載于:https://www.cnblogs.com/riasky/p/3430770.html
總結
- 上一篇: 适定、超定和欠定方程的概念
- 下一篇: 一周总结(3)