The STL deque
(double-ended queue) is a dynamic container that allows fast insertion and deletion from both ends. It is more flexible than vector
and does not require contiguous memory allocation.
✅ Key Features:
✔ Fast Insertions/Deletions at both front and back.
✔ Random Access – Supports deque[i]
.
✔ Resizable – Grows/shrinks dynamically.
✔ Efficient Memory Management – No need for contiguous allocation.
1️⃣ Syntax & Declaration
#include <iostream>
#include <deque> // Include deque library
using namespace std;
int main() {
deque<int> dq = {10, 20, 30, 40, 50}; // Declare and initialize
cout << "First Element: " << dq.front() << endl;
cout << "Last Element: " << dq.back() << endl;
return 0;
}
🔹 Output
First Element: 10
Last Element: 50
✅ Unlike vector
, deque
allows fast front insertions! 🚀
2️⃣ Adding & Removing Elements
🔹 Adding Elements
deque<int> dq;
dq.push_back(10); // Add at the back
dq.push_front(20); // Add at the front
🔹 Removing Elements
dq.pop_back(); // Removes last element
dq.pop_front(); // Removes first element
✅ Unlike vector
, deque
supports push_front()
and pop_front()
.
3️⃣ Traversing a deque
🔹 Using a Simple Loop
for (int i = 0; i < dq.size(); i++) {
cout << dq[i] << " ";
}
🔹 Using a Range-Based Loop
for (int val : dq) {
cout << val << " ";
}
🔹 Using Iterators
for (auto it = dq.begin(); it != dq.end(); it++) {
cout << *it << " ";
}
✅ Supports iterators just like vector
!
4️⃣ Common deque
Functions
🔹 Size & Capacity
cout << "Size: " << dq.size() << endl;
🔹 Accessing Elements
cout << dq.front(); // First element
cout << dq.back(); // Last element
cout << dq.at(2); // Safe access
🔹 Checking if Empty
if (dq.empty()) cout << "Deque is empty";
🔹 Insert at Specific Position
dq.insert(dq.begin() + 1, 15); // Insert 15 at index 1
🔹 Erase an Element
dq.erase(dq.begin() + 1); // Removes element at index 1
✅ Faster than vector
for front operations!
5️⃣ Sorting a deque
#include <iostream>
#include <deque>
#include <algorithm> // Sorting functions
using namespace std;
int main() {
deque<int> dq = {50, 10, 40, 20, 30};
sort(dq.begin(), dq.end()); // Sort in ascending order
cout << "Sorted Deque: ";
for (int val : dq) cout << val << " ";
return 0;
}
🔹 Output
Sorted Deque: 10 20 30 40 50
✅ Uses sort()
from <algorithm>
.
6️⃣ Swapping Two deque
s
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq1 = {1, 2, 3};
deque<int> dq2 = {7, 8, 9};
dq1.swap(dq2); // Swap contents
cout << "After swapping:\n";
for (int val : dq1) cout << val << " "; // 7 8 9
cout << endl;
for (int val : dq2) cout << val << " "; // 1 2 3
return 0;
}
✅ swap()
exchanges contents efficiently.
7️⃣ Clearing a deque
dq.clear(); // Removes all elements
✅ Memory remains allocated. To free memory, use:
deque<int>().swap(dq);
8️⃣ Summary
✔ std::deque<T>
– Dynamic, double-ended queue.
✔ Supports push_back()
, push_front()
, pop_back()
, pop_front()
.
✔ Fast insertions/removals at both ends (better than vector
).
✔ Random access using dq[i]
, at(i)
, front()
, back()
.
✔ Sorting via sort(dq.begin(), dq.end())
.
✔ Resizable with resize()
, clear()
, swap()
.
Would you like an STL list
next? 🚀
0 Comments