First Half by Iterator Range
mediumQuestion 569Write a function vector<int> firstHalf(const vector<int>& v) that returns a new vector containing the first v.size() / 2 elements of v (integer division, so for an odd size the middle element is left out). Build the result with the range constructor vector<int>(first, last) and iterator arithmetic — do not push elements one by one. For an empty vector return an empty vector.
The test prints the returned vector's elements separated by spaces on one line. For {1, 2, 3, 4} it prints 1 2; for {10, 20, 30, 40, 50} it prints 10 20.
Do NOT write a main function.
Expected output
1 2 10 20
= newline. The output ends with a newline, so 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.