← Back to STL Containers
Question 560
Sum Between Two Iterators
Sum Between Two Iterators
Write a functionint sumRange(vector<int>::iterator first, vector<int>::iterator last)that returns the sum of every element in the half-open range fromfirsttolast. Iffirst == lastthe range is empty and the sum is 0. Do not use any container inside the function — walk fromfirsttowardlastwith the iterators you were given. The test creates the vector {1, 2, 3, 4, 5}, callssumRange(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
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.