C++ Program to Illustrate Usage of Deque

This C++ program illustrates usage of deque. Deques is acronym for double-ended queues with dynamic sizes that can be expanded or contracted on both ends. It provides efficient insertion and removal at both of its ends but unlike vectors, the elements are not stored in contiguous storage locations.

Here is the source code of the C++ program which illustrates usage of deque. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to illustrate usage of deque
  3.  */
  4. #include <iostream>
  5. #include <deque>
  6. using namespace std;
  7.  
  8. void printDeque(deque<int> d)
  9. {
  10.     cout << "Deque : ";
  11.     for(deque<int>::const_iterator i = d.begin(); i != d.end(); i++)
  12.         cout << *i << " ";
  13.     cout << "\n";
  14. }
  15.  
  16. int main() {
  17.     deque<int> d = {10, 20, 30, 40, 50, 60};
  18.  
  19.     printDeque(d);
  20.     cout << "Inserting 0 at the front\n";
  21.     d.push_front(0);
  22.     printDeque(d);
  23.     cout << "Inserting 70 at the back\n";
  24.     d.push_back(70);
  25.     printDeque(d);
  26. }

$ gcc test.cpp
$ a.out
Deque : 10 20 30 40 50 60 
Inserting 0 at the front
Deque : 0 10 20 30 40 50 60 
Inserting 70 at the back
Deque : 0 10 20 30 40 50 60 70

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.