How to Use deque in C++: Syntax, Example, and Tips
In C++,
deque is a double-ended queue container that allows fast insertion and deletion at both ends. You include <deque>, declare a std::deque<Type>, and use methods like push_back, push_front, pop_back, and pop_front to manage elements.Syntax
The deque container is declared as std::deque<Type> name;. You can add elements at the back with push_back() or at the front with push_front(). Remove elements with pop_back() or pop_front(). Access elements using operator[] or at().
cpp
#include <deque> #include <iostream> int main() { std::deque<int> d; d.push_back(10); // Add 10 at the back d.push_front(5); // Add 5 at the front std::cout << d[0] << " " << d.at(1) << std::endl; // Access elements d.pop_back(); // Remove from back d.pop_front(); // Remove from front return 0; }
Output
5 10
Example
This example shows how to create a deque, add elements to both ends, access them, and remove elements. It prints the contents before and after removals.
cpp
#include <deque> #include <iostream> int main() { std::deque<std::string> dq; dq.push_back("apple"); dq.push_front("banana"); dq.push_back("cherry"); std::cout << "Contents of deque:" << std::endl; for (const auto& fruit : dq) { std::cout << fruit << " "; } std::cout << std::endl; dq.pop_front(); // removes "banana" dq.pop_back(); // removes "cherry" std::cout << "After popping front and back:" << std::endl; for (const auto& fruit : dq) { std::cout << fruit << " "; } std::cout << std::endl; return 0; }
Output
Contents of deque:
banana apple cherry
After popping front and back:
apple
Common Pitfalls
One common mistake is using operator[] without checking size, which can cause undefined behavior if the index is out of range. Use at() for bounds-checked access. Another pitfall is confusing push_back and push_front when adding elements, which changes the order unexpectedly.
cpp
#include <deque> #include <iostream> int main() { std::deque<int> d = {1, 2, 3}; // Wrong: accessing out of range with operator[] (no error but undefined behavior) // std::cout << d[5] << std::endl; // Unsafe! // Right: use at() to get exception if out of range try { std::cout << d.at(5) << std::endl; } catch (const std::out_of_range& e) { std::cout << "Out of range error caught: " << e.what() << std::endl; } // Confusing push_front and push_back d.push_front(0); // adds 0 at front d.push_back(4); // adds 4 at back for (int n : d) std::cout << n << " "; std::cout << std::endl; return 0; }
Output
Out of range error caught: deque::_M_range_check: __n (which is 5) >= this->size() (which is 3)
0 1 2 3 4
Quick Reference
| Operation | Method | Description |
|---|---|---|
| Add at back | push_back(value) | Insert element at the end |
| Add at front | push_front(value) | Insert element at the beginning |
| Remove from back | pop_back() | Remove last element |
| Remove from front | pop_front() | Remove first element |
| Access element | operator[](index) or at(index) | Get element at position |
| Check size | size() | Get number of elements |
| Check empty | empty() | Returns true if deque is empty |
Key Takeaways
Use
std::deque<Type> to create a double-ended queue in C++.Add or remove elements efficiently at both front and back using
push_front, push_back, pop_front, and pop_back.Use
at() for safe element access to avoid out-of-range errors.Remember that
deque allows random access like a vector but is optimized for fast insertions/removals at both ends.Always include
<deque> header and use the std namespace or prefix.