← Back to Streams
Question 631
Print a Vector to a Stream
Print a Vector to a Stream
Write a functionvoid printVec(ostream& out, const vector<int>& v)that writes the elements ofvinside square brackets, separated by a comma and a space, followed by a newline. The loop and the brackets are already in the starter; fill in the two commented lines so that the separator is printed before every element except the first. An empty vector prints just[]. Output ofprintVec(cout, {1, 2, 3})(see Expected Output). Do NOT write a main function.
Expected Output:
[1, 2, 3] [] [42]
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsostream
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.