← Back to STL Containers
Question 541
Index of a Value
Index of a Value
Write a functionint indexOf(const array<int, 6>& a, int x)that returns the index (0 to 5) of the first element equal tox, or -1 ifxdoes not appear in the array. Ifxappears more than once, return the smallest index. For {4, 8, 15, 16, 23, 42} andx = 15the function returns 2. Each test prints the returned indexes on their own lines, as shown in Expected Output. Do NOT write a main function.
Expected Output:
2 -1 1 -1
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL Containersstd::array
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.