← Back to STL Containers
Question 566
Index of the Maximum
Index of the Maximum
Write a functionint indexOfMax(const vector<int>& v)that returns the 0-based index of the largest element invusingmax_element(from<algorithm>). If the largest value appears more than once, return the index of its first occurrence. You may assumevis not empty. The test prints the returned index on its own line. For {1, 5, 2, 9} it prints3; for {9, 1, 9} it prints0. Do NOT write a main function.
Expected Output:
3 0
= 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.