sahilbansal17 / Competitive_Coding

This repository contains some useful codes, techniques, algorithms and problem solutions helpful in Competitive Coding.
GNU General Public License v3.0
407 stars 308 forks source link

Error in template #614

Closed harshraj22 closed 4 years ago

harshraj22 commented 4 years ago

https://github.com/sahilbansal17/Competitive_Coding/blob/1403a763003397ef2f41baac8a7d19f958f16f76/cpp/array_helper.cpp#L20

The last argument in accumulate (i.e. 0 in this case) should have same data type as that of array. So the following code fails (gives compilation error)

vector<long long int> v = {1, 3};
cout << accumulate(v.begin(), v.end(), 0) << '\n';

while the following passes

vector<long long int> v = {1, 3};
cout << accumulate(v.begin(), v.end(), 0LL) << '\n';

In template, we are accepting the type of elements that vector contains, but using accumulate as if vector contains only integers.

sahilbansal17 commented 4 years ago

@harshraj22 Oh, got it, thanks for pointing this out, can you find a fix for this?

harshraj22 commented 4 years ago

Somehow the template seems to be working, which I can't figure out why. Hence closing.