C++ deques
In C++, a deque (short for "double-ended queue") is a container in the C++ Standard Library that allows for efficient insertion and removal of elements from both ends. It is defined in the <deque> header and is part of the sequence containers in C++. Unlike a regular queue (std::queue
), which allows insertion and removal only from the front and back, a deque allows inserting and deleting elements from both the front and the back efficiently.
Internally, std::deque
is implemented as a dynamic array of blocks, which allows elements to be stored in a non-contiguous manner while providing constant-time access to both ends of the container.
std::deque
std::deque
is implemented using blocks of arrays, unlike vectors which store elements contiguously.To declare a deque:
std::deque<T> deque_name;
Where:
T
: Type of the elements (e.g., int
, std::string
).You can also initialize a deque with specific elements:
std::deque<T> deque_name = {element1, element2, element3};
#include <iostream>
#include <deque>
int main() {
// Declare and initialize a deque of integers
std::deque<int> dq = {1, 2, 3, 4, 5};
// Displaying elements using a range-based for loop
for (int i : dq) {
std::cout << i << " ";
}
return 0;
}
Explanation:
dq
is initialized with the values {1, 2, 3, 4, 5}
.for
loop is used to iterate through the deque and print the elements.std::deque
std::deque
provides several member functions for performing operations like insertion, deletion, and access.
push_back()
and push_front()
push_back()
) or the front (push_front()
) of the deque.
dq.push_back(6); // Adds 6 to the back of the deque
dq.push_front(0); // Adds 0 to the front of the deque
pop_back()
and pop_front()
pop_back()
) or the front (pop_front()
) of the deque.
dq.pop_back(); // Removes the element at the back
dq.pop_front(); // Removes the element at the front
front()
and back()
front()
) and back (back()
) elements of the deque.
std::cout << "Front element: " << dq.front() << std::endl; // Access front element
std::cout << "Back element: " << dq.back() << std::endl; // Access back element
size()
std::cout << "Size of deque: " << dq.size() << std::endl;
empty()
if (dq.empty()) {
std::cout << "Deque is empty!" << std::endl;
} else {
std::cout << "Deque is not empty!" << std::endl;
}
insert()
std::deque<int>::iterator it = dq.begin();
dq.insert(it + 2, 99); // Inserts 99 at the 3rd position
erase()
dq.erase(it); // Removes the element at position `it`
clear()
dq.clear(); // Clears the entire deque
resize()
dq.resize(10); // Resizes the deque to hold 10 elements
You can iterate through a std::deque
using various methods, such as traditional loops, range-based for loops, or iterators.
for
Loop with Iterators
#include <iostream>
#include <deque>
int main() {
std::deque<int> dq = {10, 20, 30, 40};
// Traditional for loop using an iterator
for (std::deque<int>::iterator it = dq.begin(); it != dq.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
Example 2: Using a Range-Based for
Loop
#include <iostream>
#include <deque>
int main() {
std::deque<int> dq = {1, 2, 3, 4, 5};
// Range-based for loop
for (int i : dq) {
std::cout << i << " ";
}
return 0;
}
std::deque
Over Other Containersstd::deque
Use std::deque
when: