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.
/*
* C++ Program to illustrate usage of deque
*/
#include <iostream>
#include <deque>
using namespace std;
void printDeque(deque<int> d)
{
cout << "Deque : ";
for(deque<int>::const_iterator i = d.begin(); i != d.end(); i++)
cout << *i << " ";
cout << "\n";
}
int main() {
deque<int> d = {10, 20, 30, 40, 50, 60};
printDeque(d);
cout << "Inserting 0 at the front\n";
d.push_front(0);
printDeque(d);
cout << "Inserting 70 at the back\n";
d.push_back(70);
printDeque(d);
}
$ 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.
Related Posts:
- Apply for Information Technology Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs