Accumulate function in Vector (STL) giving negative sum

  accumulate, c++, stl, vector

When i am coding the code below, i am getting a negative sum (-294967296).

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
    vector<long long int> v={1000000000 , 1000000000 , 1000000000 , 1000000000};
    cout<<"Sum of all the elements are:"<<endl;
    cout<<accumulate(v.begin(),v.end(),0);
}

but when i am coding the code below i am getting a positive sum (2000000000)

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
    vector<long long int> v={1000000000 , 1000000000};
    cout<<"Sum of all the elements are:"<<endl;
    cout<<accumulate(v.begin(),v.end(),0);
}

What might be the reason?

Source: Windows Questions C++

LEAVE A COMMENT