← Back to STL Containers
Question 569
First Half by Iterator Range
First Half by Iterator Range
Write a functionvector<int> firstHalf(const vector<int>& v)that returns a new vector containing the firstv.size() / 2elements ofv(integer division, so for an odd size the middle element is left out). Build the result with the range constructorvector<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 prints1 2; for {10, 20, 30, 40, 50} it prints10 20. Do NOT write a main function.
Expected Output:
1 2 10 20
= newline. The output ends with a newline — 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.