CJCoding With Joseph
15free / daySign inCreate account

Insert Before a Value

Write 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 — print one after the last line.

Hints4 available
Topics:
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.