The output is 705032704 instead of 5000000000. Why is that? I thought std::accumulate
would compute the sum of the elements in the vector.
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <iterator>
#include <queue>
#include <stack>
#include <numeric>
typedef long long ll;
int main()
{
std::vector<ll> nums = {1000000000, 1000000000,1000000000,1000000000,1000000000};
std::cout << std::accumulate(nums.begin(), nums.end(), 0);
std::cin.ignore();
}
The standard algorithm std::accumulate
is declared the following way
template<class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
So the template parameter T
is used as the type of the accumulator.
In this call
std::accumulate(nums.begin(), nums.end(), 0)
the deduced type of the parameter T is int
because the integer literal 0
has the type int
. As a result there is an overflow of the sum stored in the accumulator of the type int
.
You have to use at least the long long integer literal 0ll
as
std::accumulate(nums.begin(), nums.end(), 0ll
)
or maybe it is reasonable to use a double floating literal
std::accumulate(nums.begin(), nums.end(), 0.0 )
Here is a demonstrative program
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
using ll = long long int;
int main()
{
std::vector<ll> nums = {1000000000, 1000000000,1000000000,1000000000,1000000000};
std::cout << std::accumulate( std::begin( nums ), std::end( nums ), 0ll ) << '\n';
std::cout << std::accumulate( std::begin( nums ), std::end( nums ), 0.0 ) << '\n';
}
Its output is
5000000000
5e+09
Integer overflow.
std::accumulate
infers the type of summing variable from the type of third argument. Since you pass in an integer, the result of std::accumulate
will also be an integer.
Pass long long
literal instead (from C++11):
std::accumulate(nums.begin(), nums.end(), 0LL);
You don't sum into a long long
but in a int
, try this
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
int main()
{
std::vector<ll> nums = {1000000000, 1000000000,1000000000,1000000000,1000000000};
std::cout << std::accumulate(nums.begin(), nums.end(), 0ll);
std::cin.ignore();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With