Evaluate Postfix
mediumQuestion 514Write a function int evalPostfix(const vector<string>& tokens) that uses a stack<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 is left 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, so 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.