Deque in STL C++

About Deque in STL :

Deque in STL is defined as the container used to insert and remove elements at both ends. They are implementing using queue data structure.

deque in stl

Introduction to Deque in C++

Deque, short for “Double-Ended Queue,” is an essential data structure in C++ provided by the Standard Template Library (STL). It shares similarities with vectors and lists but offers additional benefits due to its versatile nature. A Deque allows elements to be inserted or removed from both ends efficiently, making it an excellent choice for various applications.

Advantages of using Deque:

Dynamic Size: Unlike arrays, Deque can change its size dynamically, making it more flexible in handling varying amounts of data.

Fast Insertions and Deletions: Deque enables fast insertions and deletions at both ends, providing efficient data manipulation.

Random Access: Like vectors, Deque allows for direct access to elements using indexing, ensuring quick element retrieval.

Memory Efficiency: Deque optimizes memory utilization, making it an ideal choice for large-scale applications.

Functions in Deque :

 FunctionWorking
push_back()insert an element to the back end of the deque.
push_front()insert an element to the front end of the deque.
size()give the number of elements present in the deque.
empty()tells whether the deque is empty or not.
max_size()

return the maximum number of elements can deque hold.

Basic Operations on Deque

Initializing a Deque:

To use a Deque, we need to include the header file in our C++ program. We can then create a Deque object using its constructor.
Run
#include < iostream >
#include < deque >

int main() {
    std::deque<int> myDeque; // Initializing an empty integer Deque
    return 0;
}

Insertion and Deletion of Elements:

Deque supports several methods for inserting and removing elements. Some commonly used functions include push_back(), push_front(), pop_back(), and pop_front(). Insertion and Deletion of Elements:
Run
#include <iostream>
#include <deque>
int main() {
    std::dequemyDeque;

    myDeque.push_back(10);    // Insert 10 at the end
    myDeque.push_front(5);    // Insert 5 at the beginning

    myDeque.pop_back();       // Remove the last element
    myDeque.pop_front();      // Remove the first element

    return 0;
}

Accessing Elements in Deque:

Elements in a Deque can be accessed using the subscript operator [] or the at() function.

Run
#include <iostream>
#include <deque>

int main() {
    std::deque<int>myDeque;

    myDeque.push_back(10);
    myDeque.push_back(20);
    myDeque.push_back(30);

    int elementAtIndex1 = myDeque[1]; // Access the element at index 1 (20)
    int elementAtIndex2 = myDeque.at(2); // Access the element at index 2 (30)

    return 0;
}

Implementation of Deque in STL

The C++ Standard Template Library (STL) provides a rich set of classes and functions to work with data structures like Deque.

Code example of Deque implementation:

Run
#include <iostream>
#include <deque>

int main() {
    std::deque<int>myDeque;

    myDeque.push_back(10);
    myDeque.push_back(20);
    myDeque.push_front(5);

    std::cout << "Front element: " << myDeque.front() << std::endl; // Output: Front element: 5
    std::cout << "Back element: " << myDeque.back() << std::endl;   // Output: Back element: 20

    return 0;
}

Use Cases of Deque in C++

Deque finds applications in various scenarios due to its unique characteristics. Real-world scenarios for Deque:

Task Scheduling: Deque can be used in task scheduling algorithms, where new tasks can be added or removed efficiently from both ends.

Sliding Window Technique: Deque is commonly used to solve problems involving sliding window algorithms, where elements move in a fixed-size window.

Undo/Redo Operations: Deque’s ability to insert and remove elements from both ends makes it suitable for implementing undo and redo functionalities in applications.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription