C++顺序容器之deque初探
生活随笔
收集整理的這篇文章主要介紹了
C++顺序容器之deque初探
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
deque是雙端隊列,與vector非常相似,是順序容器,不同的是,deque可以在數(shù)組開頭和末尾插入和刪除數(shù)據(jù)。支持快速隨機訪問。
#include<iostream> #include<deque> #include<algorithm> using namespace std; int main() {deque<int> a;// 在末尾插入數(shù)據(jù)a.push_back(3);a.push_back(4);a.push_back(5);// 在開頭插入數(shù)據(jù)a.push_front(2);a.push_front(1);a.push_front(0);// 以數(shù)組方式輸出for (size_t n = 0; n < a.size(); ++n)cout << "a[" << n << "] = " << a[n] << endl;cout << endl;a.pop_back();a.pop_front();// 以迭代器方式輸出deque<int>::iterator iter;for (iter = a.begin(); iter < a.end(); ++iter){// 計算數(shù)組下標,distance包含在algorithm中int index = distance(a.begin(), iter);cout << "a[" << index << "] = " << *iter << endl;}system("pause");return 0; }程序輸出結(jié)果:
a[0] = 0 a[1] = 1 a[2] = 2 a[3] = 3 a[4] = 4 a[5] = 5a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 請按任意鍵繼續(xù). . .?
總結(jié)
以上是生活随笔為你收集整理的C++顺序容器之deque初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 出现错误 Duplica
- 下一篇: s3c2440移植MQTT