Insert Before a Value
mediumQuestion 562Write a function void insertBefore(vector<int>& v, int target, int val) that:
1. searches v for the first element equal to target using find;
2. if it is found, inserts val immediately before that element;
3. if it is not found, appends val to the end of v.
The test prints the vector's elements separated by spaces on one line. For {1, 2, 3} with target = 2, val = 9 the line is 1 9 2 3; for {1, 2, 3} with target = 7, val = 9 the line is 1 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, so 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.