Sum Between Two Iterators
mediumQuestion 560Write a function int sumRange(vector<int>::iterator first, vector<int>::iterator last) that returns the sum of every element in the half-open range from first to last. If first == last the range is empty and the sum is 0. Do not use any container inside the function — walk from first toward last with the iterators you were given.
The test creates the vector {1, 2, 3, 4, 5}, calls sumRange(v.begin() + 1, v.begin() + 4) (the elements 2, 3, 4) and prints the result on its own line.
Do NOT write a main function.
Expected output
9 0
= 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.