Deque
Lesson 6 of 23 in Coddy's C++ - Standard Template Library course.
Deque is a sequence container in C++ also referred to as double-ended queue. You will learn about a regular queue later in the course in the chapter about Container Adaptors. But for now, you should know that in a regular queue, the elements are inserted from the back and they are removed from the front. But, in a deque we are able to insert and remove elements from both the front and back.

We implement the deque data structure by including the library at the top of our .cpp file with #import <deque> (we can also access the deque by including the bits/stdc++.h header file)
Next, we create a deque with the following syntax:
deque<data_type> deque_name;First, we use the deque keyword. Second, we put the variable type that we want to store inside the deque and we finally specify the deque variable name.
deque<int> numbers;We can also initialize a deque when we are declaring it with the same syntax that we used for the vector data structure:
deque<int> numbers = {1, 2, 3, 4, 5};
cout << numbers[0];Output:
1So, we insert element from the back of the deque using the push_back() method and we insert the up front with the push_front() method.
deque<int> numbers = {2, 3};
numbers.push_front(1);
numbers.push_back(4);
for(int i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";Output:
1 2 3 4Deque Methods
| Method | Functionality |
| push_back() | Inserts a new element at the back |
| push_front() | Inserts a new element at the front |
| pop_back() | Removes the element from the back |
| pop_front() | Removes the element from the front |
| size() | Returns the number of elements |
| empty() | Returns whether the deque is empty |
| clear() | Removes all the elements of the deque |
Challenge
EasyUser will enter positive numbers until they enter -1. Iterate inserting each number in a deque beginning with the first one in front, the second at the back, the third one at front, etc... After that using a loop iterate through the deque and output every element with one space in between of the elements.
Input
5
10
15
20
25
30
-1Output
25 15 5 10 20 30Try it yourself
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Enter your code here
return 0;
}