/* A deque is a dynamic array whose size can be efficiently
changed at both ends of the array. Like a vector, a deque provides
the functions push_back and pop_back, but it also includes the
functions push_front and pop_front. */
deque<int> d;
d.push_back(5); // [5]
d.push_back(2); // [5,2]
d.push_front(3); // [3,5,2]
d.pop_back(); // [3,5]
d.pop_front(); // [5]
// g++ std-deque.cpp -o a.out -std=c++11
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {1, 2, 3, 4}; // [1, 2, 3, 4]
d.push_back(5); // [1, 2, 3, 4, 5]
d.pop_front(); // [2, 3, 4, 5]
d.push_front(0); // [0, 2, 3, 4, 5]
d.pop_back(); // [0, 2, 3, 4]
// 印出 deque 內所有內容, c++11 才支援
for (int &i : d) {
cout << i << " ";
}
cout << "
";
cout << d[0] << " " << d[1] << " " << d[2] << "
";
return 0;
}
// CPP Program to implement Deque in STL
#include <deque>
#include <iostream>
using namespace std;
void showdq(deque<int> g)
{
deque<int>::iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << ' ' << *it;
cout << '
';
}
int main()
{
deque<int> gquiz;
gquiz.push_back(10);
gquiz.push_front(20);
gquiz.push_back(30);
gquiz.push_front(15);
cout << "The deque gquiz is : ";
showdq(gquiz);
cout << "
gquiz.size() : " << gquiz.size();
cout << "
gquiz.max_size() : " << gquiz.max_size();
cout << "
gquiz.at(2) : " << gquiz.at(2);
cout << "
gquiz.front() : " << gquiz.front();
cout << "
gquiz.back() : " << gquiz.back();
cout << "
gquiz.pop_front() : ";
gquiz.pop_front();
showdq(gquiz);
cout << "
gquiz.pop_back() : ";
gquiz.pop_back();
showdq(gquiz);
return 0;
}