Mega Code Archive

 
Categories / C++ Tutorial / Deque
 

Deque

#include <iostream> #include <deque> using namespace std; void show(const char *msg, deque<int> q); int main() {   deque<int> dq(10);   for(unsigned i=0; i < dq.size(); ++i) dq[i] = i*i;   show("Contents of dq: ", dq);   int sum = 0;   for(unsigned i=0; i < dq.size(); ++i) sum += dq[i];   double avg = sum / dq.size();   cout << avg << endl;   // Add elements to the end of dq.   dq.push_back(100);   dq.push_back(121);   show("dq after pushing elements onto the end: ", dq);   // Now use pop_back() to remove one element.   dq.pop_back();   show("dq after back-popping one element: ", dq);   cout << *dq.begin() << ", " << *(dq.end()-1) << "\n\n";   cout << *dq.rbegin() << ", " << *(dq.rend()-1) << "\n\n";   return 0; } // Display the contents of a deque<int>. void show(const char *msg, deque<int> q) {   cout << msg;   for(unsigned i=0; i < q.size(); ++i)     cout << q[i] << " ";   cout << "\n"; }