A vector is a convenient growable sequence when most additions happen at its back. A deque, pronounced “deck” and short for double-ended queue, is designed for efficient insertion and removal at both ends. std::deque<T> is declared in <deque>.
Use push_front(value) to place a value before the current first element and push_back(value) to place one after the current last element. A deque keeps the resulting sequence; unlike a set, it neither sorts values nor removes duplicates. You can read it with a range loop or a valid numeric index, but its storage is not one contiguous array like a vector's.
front() and back() read the end values without removing them. pop_front() and pop_back() remove values and return nothing. Check empty() before reading or removing when there might be no elements. Adding to either end is valid even when the deque starts empty. Do not keep using a saved iterator across edits that may invalidate it; finish the changes before starting this lesson's traversal.
#include <deque>
#include <iostream>
int main() {
std::deque<int> stops{20, 30};
stops.push_front(10);
stops.push_back(40);
for (int stop : stops) { std::cout << stop << '\n'; }
}
The example prints 10, 20, 30, and 40; the original middle values remain in order. Unlike the queue in the previous lesson, a deque lets you add at either end. Your starter already contains Sort followed by Pack. Insert the urgent job before them and the closing job after them, rather than replacing either existing entry. The two output lines let you check each insertion separately.
push_front to add "Inspect" before the existing jobs. The first probe should print First: Inspect.push_back to add "Label" after the existing jobs. The last probe should print Last: Label.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.