CJCoding With Joseph
15free / daySign inCreate account

Sum Between Two Iterators

Write 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 — 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.