← Back to STL Containers
Question 514
Evaluate Postfix
Evaluate Postfix
Write a functionint evalPostfix(const vector<string>& tokens)that uses astack<int>to evaluate an expression written in postfix (Reverse Polish) notation. Each token is either an integer written as text (such as "2" or "-5") or one of the four operators+,-,*,/. Pop the right operand first and the left operand second, so the result isleft op right. Division is integer division. The expression is always valid. Example: the tokens {"2", "3", "4", "*", "+"} mean 2 + (3 * 4). The test prints the returned value on its own line (see Expected Output). Do NOT write a main function.
Expected Output:
14 3 -3
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.