I have an assignment that is the following:
For a given integer array, find the sum of its elements and print out the final
result, but to get the sum, you need to execute the function for_each() in STL
only once (without a loop).
As of now this is my code:
void myFunction (int i) {
cout << " " << i << " " << endl;
}
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
vector<int> v(array[0], array[10]);
for_each( v.begin(), v.end(), myFunction);
return 0;
}
But for some reason the output shows up as 4198853, at first I thought it was a memory address but I figured out that was wrong. Any idea’s as to what I might be doing wrong?
why not just:
I’m quite sure that
int*can be used as iteratorEDIT: just checked this, it can indeed