c++ - Why does std::accumulate behave like this with standard arrays? -
I'm just coming in C ++ and I think I have handle pointer, but std: : Accumulate () has confused me.
Looking at the array:
int a [3] = {5, 6, 7}; I want to add values of the array to std :: accumulate () , so I give it an indicator for the first element, then the last, Then the initial value of the receiver.
std :: cached (a, a + 2, 0); Std :: deposits (& amp; a [0], & a [2], 0); Oh: None of these gives the sum of the first two elements: 11 . On the other hand, if the second argument is a nonsensical indicator, just outside the limit ...
std :: deposits (a, a + 3, 0); Std :: deposits (& amp; a [0], & a [3], 0); The correct value of
... 18 has been returned.
Can anyone please explain this? I know that I can not use simple arrays, but it's next to the point.
C ++ categories are defined [first, last] , And all STL algorithms work that way. In this case, all the elements behind the std :: accumulate defined class, starting with first and without actually last Ends denote it.
As such, calling it like std :: accumulate (a, a + 3, 0) is really correct and calling it with std :: Is equal to (start (a), end (a), 0) .
Also note that this is a typical exception to the pointer behind the last element, "There is no sign outside the allocated array".
Comments
Post a Comment