In this tutorial, we will explore the deque (double-ended queue) container in C++ STL. A deque is a sequence container that allows fast insertion and deletion at both ends. It provides dynamic size arrays with efficient access to elements from both ends.
The deque container is part of the Standard Template Library (STL) in C++. It stands for "double-ended queue" because it supports fast insertions and deletions at both the front and back. This makes it different from a vector, which only allows efficient operations at the back. Deques are particularly useful when you need to frequently add or remove elements from either end of the container.
A deque is a sequence container that supports fast insertions and deletions at both the front and back. It is implemented as a series of chunks or blocks, which allows for efficient access to elements from both ends. This makes deques ideal for applications where you need frequent modifications at both ends.
You can create a deque using the std::deque template class. Here's how you can declare and initialize a deque:
1#include <iostream>2#include <deque>34int main() {5std::deque<int> dq = {1, 2, 3, 4, 5};67for (const auto& elem : dq) {8std::cout << elem << " ";9}1011return 0;12}
1 2 3 4 5
You can add elements to a deque using push_front() and push_back() methods. These methods allow you to insert elements at the front and back of the deque, respectively.
1#include <iostream>2#include <deque>34int main() {5std::deque<int> dq = {1, 2, 3};67dq.push_front(0); // Add element at the front8dq.push_back(4); // Add element at the back910for (const auto& elem : dq) {11std::cout << elem << " ";12}1314return 0;15}
0 1 2 3 4
You can remove elements from a deque using pop_front() and pop_back() methods. These methods allow you to delete elements from the front and back of the deque, respectively.
1#include <iostream>2#include <deque>34int main() {5std::deque<int> dq = {0, 1, 2, 3, 4};67dq.pop_front(); // Remove element from the front8dq.pop_back(); // Remove element from the back910for (const auto& elem : dq) {11std::cout << elem << " ";12}1314return 0;15}
1 2 3
One of the advantages of deques is that they support random access, similar to vectors. You can access elements by their index using the [] operator or the at() method.
1#include <iostream>2#include <deque>34int main() {5std::deque<int> dq = {10, 20, 30, 40, 50};67// Accessing elements using the [] operator8std::cout << "Element at index 2: " << dq[2] << std::endl;910// Accessing elements using the at() method11try {12std::cout << "Element at index 4: " << dq.at(4) << std::endl;13} catch (const std::out_of_range& e) {14std::cerr << "Out of range error: " << e.what() << std::endl;15}1617return 0;18}
Element at index 2: 30 Element at index 4: 50
| Feature | deque | vector |
|---|---|---|
| Insertion/Deletion | Fast at both ends | Fast only at the back |
| Memory Layout | Non-contiguous, multiple blocks | Contiguous memory allocation |
| Random Access | Supported | Supported |
| Use Case | Frequent insertions/deletions at both ends | Fixed-size or rarely modified data |
Tip
Let's create a practical example that demonstrates the use of a deque. We'll implement a simple application that simulates a queue with frequent insertions and deletions at both ends.
1#include <iostream>2#include <deque>34int main() {5std::deque<int> dq;67// Adding elements to the deque8dq.push_back(1);9dq.push_back(2);10dq.push_front(0);11dq.push_front(-1);1213std::cout << "Deque after initial insertions: ";14for (const auto& elem : dq) {15std::cout << elem << " ";16}17std::cout << std::endl;1819// Removing elements from the deque20dq.pop_back();21dq.pop_front();2223std::cout << "Deque after removals: ";24for (const auto& elem : dq) {25std::cout << elem << " ";26}27std::cout << std::endl;2829return 0;30}
Deque after initial insertions: -1 0 1 2 Deque after removals: 0 1
In the next tutorial, we will explore the stack container in C++ STL. A stack is a LIFO (Last In, First Out) data structure, which is useful for scenarios where you need to process items in reverse order. Stay tuned!