Some collections restrict how you access values because the order of work matters. A stack works like a pile of trays: the last value pushed is the first one available to remove. This is last-in-first-out, or LIFO. std::stack<T> is declared in <stack>; use push(value), top(), and pop().
A queue works like a line at a desk: the first value pushed is the first one available to remove. This is first-in-first-out, or FIFO. std::queue<T> is declared in <queue>; use push(value), front(), and pop().
For both types, reading and removing are separate operations. top() or front() reads the next value without removing it. pop() removes that value and returns no value. Call empty() before reading or removing if the collection might have no elements. After removal, the next read refers to a different item, provided one remains.
#include <iostream>
#include <stack>
#include <queue>
int main() {
std::stack<int> trays;
trays.push(10);
trays.push(20);
std::queue<int> tickets;
tickets.push(10);
tickets.push(20);
std::cout << trays.top() << ' ' << tickets.front() << '\n';
}
The example prints 20 10: the stack exposes the latest insertion, while the queue exposes the earliest. These are called container adapters because they restrict how an underlying container is used. In the starter, both collections receive the same two numbers. Call pop() once at each marker, then compare the next values. Do not try to print the result of pop() itself; the supplied top() and front() calls read what remains.
pop() on the nonempty undo stack to remove its newest item. Its next value should print Undo next: 3.pop() on the nonempty waiting queue to remove its oldest item. Its next value should print Waiting next: 7.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.