← Back to STL Containers
Question 563
Second Largest with Iterator Arithmetic
Second Largest with Iterator Arithmetic
Write a functionint secondLargest(vector<int> v)that returns the second-largest value inv. The vector is passed BY VALUE, so sort it ascending in place withsort, then return the element two positions beforeend(). You may assume the vector always has at least 2 elements. Duplicates count as separate positions: for {4, 9, 9} the answer is 9. The test prints the returned value on its own line. For {3, 7, 9, 1} it prints7. Do NOT write a main function.
Expected Output:
7 2
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
STL ContainersIterators
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.