Deque in C++
×

Deque in C++

110

What is a Deque?

A deque (short for double-ended queue) is a sequence container in C++ that allows insertion and deletion of elements from both the front and the back. It is part of the C++ Standard Template Library (STL) and provides fast and efficient operations at both ends.

Why Use Deque?

Deques are useful when you need to perform insertions and deletions at both ends of a sequence. Unlike vectors, which allow efficient insertions and deletions only at the end, deques provide constant time complexity for these operations at both ends.

Syntax

To declare a deque in C++, you need to include the deque header:

#include <deque>
std::deque<data_type> deque_name;
    

Example

Here's a simple example demonstrating the use of a deque:

#include <iostream>
#include <deque>

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

    myDeque.push_back(10);    // Insert at the back
    myDeque.push_front(20);   // Insert at the front
    myDeque.push_back(30);    // Insert at the back

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

    myDeque.pop_front();      // Remove from the front
    myDeque.pop_back();       // Remove from the back

    std::cout << "Size after pop operations: " << myDeque.size() << std::endl;

    return 0;
}
    

Commonly Used Functions

  • push_back(): Adds an element to the end.
  • push_front(): Adds an element to the beginning.
  • pop_back(): Removes the last element.
  • pop_front(): Removes the first element.
  • front(): Accesses the first element.
  • back(): Accesses the last element.
  • size(): Returns the number of elements.
  • empty(): Checks if the deque is empty.
  • clear(): Removes all elements.
  • at(index): Accesses the element at the specified index.

Advantages of Using Deque

  • Efficient Insertions/Deletions: Constant time complexity for insertions and deletions at both ends.
  • Dynamic Size: Automatically resizes as elements are added or removed.
  • Random Access: Allows direct access to elements using indices.

When to Use Deque

Use a deque when you need efficient insertions and deletions at both the front and back of a sequence. It's ideal for scenarios like implementing queues, sliding window algorithms, and other data structures that require frequent additions and removals at both ends.

Conclusion

The deque container in C++ STL is a versatile and efficient data structure that provides fast insertions and deletions at both ends. Understanding how to use deques effectively can help you write more efficient and cleaner code for various applications.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat