std::deque (Double-Ended Queue) is an indexed sequence container that allows fast $O(1)$ constant-time insertion and deletion at both the beginning and the end.
#include <iostream>
#include <deque>
int main() {
std::deque<int> dq;
dq.push_back(100); // Insert at back
dq.push_front(50); // Insert at front: O(1)!
dq.push_back(150);
// Random access indexing works just like an array:
std::cout << "First: " << dq[0] << "\n"; // 50
std::cout << "Middle: " << dq[1] << "\n"; // 100
std::cout << "Last: " << dq[2] << "\n"; // 150
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Single contiguous buffer vs deque multiple chunked buffers.
Understanding why std::deque is the default underlying container for stack.
Queue operations built on std::deque.