← C++ from Scratch Lesson 55 of 62 · 10 XP

stack

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

std::stack is a pile of plates: put one on top (push), look at the top (top), take it off (pop). Last in, first out (LIFO).

#include <stack>

std::stack<int> s;
s.push(1); s.push(2);
s.top();    // 2
s.pop();    // now 1 is on top
s.empty();  s.size();

Where you meet it: undoing actions (Ctrl+Z), function calls (the very same stack), depth-first traversal, checking brackets, evaluating expressions.

Task. Checking brackets: for each line of the input, print yes if all the brackets ()[]{} are closed correctly and in the right order, otherwise no. Ignore other characters.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.