Open In App

Deque in C++ STL

Last Updated : 27 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.

Example:

C++
#include <iostream>
#include <deque>
using namespace std;

int main() {
    
    // Creating a deque of 5 elements
    deque<int> dq = {1, 4, 2, 3, 5};
    
    for (auto x: dq)
        cout << x << " ";
    return 0;
}

Output
1 4 2 3 5 

Explanation: In the above program, we created a deque of integers that contains 5 elements {1, 4, 2, 3, 5}.

Syntax

Deque is defined as the std::deque class template inside the <deque> header file.

deque<T> dq;

where,

  • T: Type of elements in deque.
  • dq: Name assigned to the deque.

Declaration and Initialization

Declaration and initialization are the process of creating an instance of std::deque class and assigning it some initial value. In C++, deque can be declared and initialized in multiple ways as shown in the below example:

C++
#include <bits/stdc++.h>
using namespace std;

void printD(deque<int>& dq) {
    for (auto x: dq) {
        cout << x << " ";
    }
    cout << '\n';
}

int main() {
    
    // Creating an empty deque
    deque<int> dq1;

    // Creating a deque with default size and value
    deque<int> dq2(3, 4);
    
    // Creating a deque from an initializer list
    deque<int> dq3 = {1, 4, 2, 3, 5};
    
    printD(dq1);
    printD(dq2);
    printD(dq3);
    return 0;
}

Output
4 4 4 
1 4 2 3 5 

Explanation: In the above program, three deques are initialized in the following ways:

  • deque<int> dq1 creates an empty deque of integers.
  • deque<int> dq2(3, 4) creates a deque with 3 elements, each initialized to the value 4.
  • deque<int> dq3 = {1, 4, 2, 3, 5} creates a deque and initializes it with the elements from the initializer list {1, 4, 2, 3, 5}.

Basic Operations

The basic operations on deque are shown below:

1. Inserting Elements

In deque, fast insertion and deletion are mainly performed at either ends using the push_back() and push_front() methods. Insertion at specific position can be done using insert() method.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    deque<int> dq = {1, 4, 2};

    // Inserting elements at back and front
    dq.push_back(5);
    dq.push_front(0);
    
    // Insert element at third position
    auto it = dq.begin() + 2;
    dq.insert(it, 11);

    for (auto x: dq) 
        cout << x << " ";
    return 0;
}

Output
0 1 11 4 2 5 

2. Accessing Elements

Just like arrays and vectors, deque elements can also be randomly accessed by their indexes using the operator []. Deque also provides the front()and back() methods to easily access the first and last element.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    deque<int> dq = {1, 4, 2, 3, 5};

    // Accessing elements
    cout << dq[2] << endl;
    cout << dq.front() << endl;
    cout << dq.back();

    return 0;
}

Output
2
1
5

3. Updating Elements

Elements in a deque can be updated by assigning the new value using assignment operator while accessing the element by index.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    deque<int> dq = {1, 4, 2, 3, 5};

    // Updating element
    dq[2] = 8;
    
    cout << dq[2]; 
    return 0;
}

Output
8

Explanation: In the above code, dq[2] = 8 directly changes the element at index 2 to 8. This can be verified in the program's output.

4. Traversing

Deque can be traversed by simply using a loop in which each element is accessed using its index. In deque, the indexes start from 0 and goes till deque.size() - 1. where the function size() returns the current number of elements in the deque.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    deque<int> dq = {1, 4, 2, 3, 5};

    // Traversing using the for loop
    for (int i = 0; i < dq.size(); i++) 
        cout << dq[i] << " ";
        
    return 0;
}

Output
1 4 2 3 5 

Explanation: In the above code, traversal through the deque is done using a for loop and index-based access dq[i] to visit each element sequentially until i is less than the size of the deque.

The other method is to just use the range based for loop like we have done in previous examples.

5. Deleting Elements

We can remove elements from the back and front of the deque using the pop_back() and pop_front() methods. For removing element at specific index, the erase() method is used.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    deque<int> dq = {1, 4, 2, 3, 5};

    // Deleting from the back and front
    dq.pop_back();
    dq.pop_front();
    dq.erase(dq.begin());
    for (int i = 0; i < dq.size(); i++) 
        cout << dq[i] << " ";
    return 0;
}

Output
2 3 

Time Complexity

The below table lists the time complexity of the above operations on deque:

OperationTime Complexity
Insert at backO(1) amortized
Insert at frontO(1) amortized

Insert at arbitrary position

O(n)

Remove from backO(1) amortized
Remove from frontO(1) amortized

Remove from arbitrary position

O(n)

Access elements at any position using indexO(1)
Update elements at any position using indexO(1)
Iterate the dequeO(n)

Other Common Operations

Deque is used in many situations for different purposes. The following examples' aim is to help you master deque beyond the basics:

Internal Working of Deque

A deque is implemented using a dynamic array or a set of fixed-size arrays. Unlike a vector, which stores elements in a contiguous memory block, a deque allows quick insertion and deletion at both ends by maintaining a multiple memory blocks. Refer to this article to know more - Internal Working of Deque

Queue vs Deque

The main difference between a queue and a deque is that:

  • In queue, elements are inserted into one end of the container and deleted from the other.
  • On the other hand, deque allow both insertion and deletion to both ends. It also allows random access.

All Member Functions of Deque

Following is the list of all member functions of std::deque class in C++:

Function

Description

push_front()

Add an element to the front of the deque.

push_back()

Adds an element to the end of the deque.

pop_front()

Remove the first element of the deque.

pop_back()

Removes the last element of the deque.

size()

Returns the number of elements in the deque.

max_size()

Returns the maximum number of elements that the deque can hold.

resize()

Changes the size of the deque.

empty()

Checks if the deque is empty.

at()

Accesses the element at a specific position, with bounds checking.

front()

Accesses the first element of the deque.

back()

Accesses the last element of the deque.

begin()

Returns an iterator pointing to the first element of the deque.

end()

Returns an iterator pointing to the beyond of the last element of the deque.

rbegin()

Returns a reverse iterator pointing to the last element of the deque.

rend()

Returns a reverse iterator pointing to the element preceding the first element of the deque.

cbegin()

Returns const_iterator to beginning 

cend()

Returns const_iterator to end

crbegin()

Returns const_reverse_iterator to reverse beginning

crend()

Returns const_reverse_iterator to reverse end

insert()

Inserts elements at a specific position in the deque.

erase()

Removes elements from a specific position or range in the deque.

swap()

Swaps the contents of the deque with another deque.

clear()

Removes all elements from the deque.

emplace()

function inserts a new element just before the specified position

emplace_front()

Constructs and inserts an element at the front of the deque.

emplace_back()

Constructs and inserts an element at the end of the deque.

assign()

Assigns new values to the deque elements by replacing old ones.

shrink_to_fit()

Reduces memory usage by freeing unused space.

get_allocator()

Returns a copy of the allocator object associated with the deque.

Article Tags :
Practice Tags :

Similar Reads