← Back to STL Containers
Question 562
Insert Before a Value
Insert Before a Value
Write a functionvoid insertBefore(vector<int>& v, int target, int val)that: 1. searchesvfor the first element equal totargetusingfind; 2. if it is found, insertsvalimmediately before that element; 3. if it is not found, appendsvalto the end ofv. The test prints the vector's elements separated by spaces on one line. For {1, 2, 3} withtarget = 2,val = 9the line is1 9 2 3; for {1, 2, 3} withtarget = 7,val = 9the line is1 2 3 9. Do NOT write a main function.
Expected Output:
1 9 2 3 1 2 3 9
= newline. The output ends with a newline — print one after the last line.
Hints4 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.