Second Largest with Iterator Arithmetic
mediumQuestion 563Write a function int secondLargest(vector<int> v) that returns the second-largest value in v. The vector is passed BY VALUE, so sort it ascending in place with sort, then return the element two positions before end(). 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 prints 7.
Do NOT write a main function.
Expected output
7 2
= newline. The output ends with a newline, so 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.