3 ms·
From the programmer’s standpoint, the reasoning goes like this. In C++, the initial value you pass to std::accumulate depends on the operator. For addition, 0 i
by Koshkin 7y ago
From the programmer’s standpoint, the reasoning goes like this. In C++, the initial value you pass to std::accumulate depends on the operator. For addition, 0 is the “natural” choice. For multiplication, it is 1. So, if you raise a number to some power using this function, you implicitly or explicitly start with 1. Followed by accumulation of the zero number of 0s this leaves you with 1.
- ken 7y agoC++ pow() is a function of doubles, not ints. You can't define it in terms of std::accumulate.
- Koshkin 7y agoThe integers are a subset of the reals, so the rule still should hold.
- nine_k 7y agoThis logic is broken. If you take 1 as the initial value, and run the operation n ≥ 1 times, the initial value is used at least once. But if you run the operation zero times, the initial value is never used, so the result is undefined.
- deleted 7y ago[deleted]
- Koshkin 7y agohttps://en.cppreference.com/w/cpp/algorithm/accumulate https://en.cppreference.com/w/cpp/algorithm/accumulate