← Back to STL Containers
Question 496
Keep Only the Last N
Keep Only the Last N
Write a functiondeque<int> keepLast(const vector<int>& v, int n)that returns a deque holding only the lastnelements ofv, in their original order. Ifvhasnor fewer elements, all of them are kept. You may assume n >= 1. The tests print the returned deque separated by spaces. For v = {1, 2, 3, 4, 5} and n = 3 the output is exactly what is shown in Expected Output. For v = {1, 2} and n = 5 the output is: 1 2 Do NOT write a main function.
Expected Output:
3 4 5 1 2
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersQueues
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.