← Back to STL Containers
Question 505
Top or None
Top or None
Write a functionstring topOrNone(const stack<int>& s). If the stack is empty, return the stringnone. Otherwise return the top element converted to a string withto_string. The tests print the returned string on its own line: for a stack whose top is 7 the output is7, and for an empty stack the output isnone. Do NOT write a main function.
Expected Output:
7 none
= newline. The output ends with a newline — print one after the last line.
Hints3 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.