Open In App

How to Declare a Deque in C++?

In C++, deques are sequence containers in which a user can insert data at both the front and the end of the container. In this article, we will learn how to declare a deque in C++.

Declaring a Deque in C++

Deque is defined as the class template std::deque in the <deque> header file. So to create a deque in C++, we first have to include the <deque> header file. Then we can declare the deque by creating an instance of the template by passing the type of element as the template parameter and assign some identifier to this intance.

Syntax to Declare a Deque

deque<dataType> dequeName;

Here,

C++ Program to Declare a Deque

The following program illustrates how we can declare a deque in C++:

// C++ Program to illustrates how we can declare a deque
#include <deque>
#include <iostream>
using namespace std;

int main()
{
    // Declaring a deque
    deque<int> dq;

    // Adding elements to the deque
    dq.push_back(1);
    dq.push_back(2);
    dq.push_back(3);
    dq.push_back(4);
    dq.push_back(5);

    // Printing deque elements
    cout << "Deque Elements:";
    for (int num : dq) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

Output
Deque Elements:1 2 3 4 5 

Time Complexity: O(N), where N is the number of deque elements.
Auxiliary Space: O(N)



Article Tags :